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

Re: Checking if a variable is exported



On Oct 22, 12:06am, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
}
} 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

This is explained in the doc for (P):

     ... if the reference is itself nested, the expression with
     the flag is treated as if it were directly replaced by the
     parameter name.  It is an error if this nested substitution
     produces an array with more than one word.  For example, if
     `name=assoc' where the parameter assoc is an associative array,
-->  then `${${(P)name}[elt]}' refers to the element of the associative
-->  subscripted `elt'.

And then later:

     Note that, unless the `(P)' flag is present, the flags and any
     subscripts apply directly to the value of the nested substitution;
     for example, the expansion ${${foo}} behaves exactly the same as
-->  ${foo}.  When the `(P)' flag is present in a nested substitution,
-->  the other substitution rules are applied to the value _before_ it
-->  is interpreted as a name, so ${${(P)foo}} may differ from
     ${(P)foo}.

So what you need is to isolate ${(P)1} from all other parameter flags
and also separate it from the subscript expression:

torch% () { echo ${(ts.-.)${(P)1}} } PATH          
scalar export special
torch% () { echo ${${(ts.-.)${(P)1}}[(I)export]} } PATH
2



Messages sorted by: Reverse Date, Date, Thread, Author