Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Checking if a variable is exported
- X-seq: zsh-users 22018
- From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@xxxxxxxxx>
- To: Martijn Dekker <martijn@xxxxxxxx>, "zsh-users@xxxxxxx" <zsh-users@xxxxxxx>
- Subject: Re: Checking if a variable is exported
- Date: Sat, 22 Oct 2016 00:06:24 +0300
- Authentication-results: mxback4h.mail.yandex.net; dkim=pass header.i=@yandex.ru
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1477083984; bh=1g/W8XRXGMpYCpRaM6HZIAZ3cw4ekFArqWIqIiHFyaA=; h=From:To:In-Reply-To:References:Subject:Message-Id:Date; b=jhS4XlF8WMig5kGqP0kbAvy2yDCRN8cXh4bE0JqFTxURySJjFxZqCQflNZ+HisRFy O1+ShAQoskyoRgXKIfpMKkLaizzt0nNqYYWby0bQXSqDDs5qN2ZdyQ7atsX+GsZ+bD hJHesb/hTe6qDJICzipTKVpkGUgfYaecj70cekH0=
- In-reply-to: <032fd4ec-89c1-9dae-a729-c440048ff3ec@inlv.org>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <032fd4ec-89c1-9dae-a729-c440048ff3ec@inlv.org>
21.10.2016, 23:34, "Martijn Dekker" <martijn@xxxxxxxx>:
> Hi all,
>
> Does zsh have a straightforward way for a script to check if a variable
> is exported? The closest-to-straightforward way I know of is to parse
> the output of 'typeset -p varname', which is hairy because the output
> might include various options to the command.
>
> Thanks,
>
> - M.
I guess shortest version is something like
zmodload zsh/parameter
is_exported() {
(( !! ${${(s.-.)parameters[$1]}[(Ie)export]} ))
}
or
is_exported() {
(( !! ${${(ts.-.)${(P)1}}[(Ie)export]} ))
}
(do not remember when (t) modifier was introduced, but AFAIR it is younger then zsh/parameter module).
---
BTW, if I use
() { echo ${(Pts.-.)1} } PATH
I get `scalar export special` like expected. But I always get return status 1 when using
() { (( !! ${${(Pts.-.)1}[(Ie)export]} )) } PATH
: enclosing this thing into additional ${} like
() { echo ${${(Pts.-.)1}} } PATH
makes zsh echo $PATH value and not `scalar export special` like expected. Removing additional ${} neither works:
() { echo ${(Pts.-.)1[1]} } PATH
echoes `s` like if it was a space-separated string and not an array.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author