From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@yandex.ru>
To: Martijn Dekker <martijn@inlv.org>,
"zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: Checking if a variable is exported
Date: Sat, 22 Oct 2016 00:06:24 +0300 [thread overview]
Message-ID: <4961661477083984@web22h.yandex.ru> (raw)
In-Reply-To: <032fd4ec-89c1-9dae-a729-c440048ff3ec@inlv.org>
21.10.2016, 23:34, "Martijn Dekker" <martijn@inlv.org>:
> 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.
next prev parent reply other threads:[~2016-10-21 21:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-21 20:27 Martijn Dekker
2016-10-21 21:00 ` Eric Cook
2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX) [this message]
2016-10-22 5:57 ` Bart Schaefer
2016-10-21 21:12 ` Mikael Magnusson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4961661477083984@web22h.yandex.ru \
--to=kp-pav@yandex.ru \
--cc=martijn@inlv.org \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).