Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Checking if a variable is exported

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]} ))


    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