* Checking if a variable is exported
@ 2016-10-21 20:27 Martijn Dekker
2016-10-21 21:00 ` Eric Cook
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Martijn Dekker @ 2016-10-21 20:27 UTC (permalink / raw)
To: zsh-users
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Checking if a variable is exported
2016-10-21 20:27 Checking if a variable is exported Martijn Dekker
@ 2016-10-21 21:00 ` Eric Cook
2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-10-21 21:12 ` Mikael Magnusson
2 siblings, 0 replies; 5+ messages in thread
From: Eric Cook @ 2016-10-21 21:00 UTC (permalink / raw)
To: zsh-users
On 10/21/2016 04:27 PM, Martijn Dekker wrote:
> 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.
>
if [[ ${(t)HOME} = *export* ]]; then ...
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Checking if a variable is exported
2016-10-21 20:27 Checking if a variable is exported Martijn Dekker
2016-10-21 21:00 ` Eric Cook
@ 2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-10-22 5:57 ` Bart Schaefer
2016-10-21 21:12 ` Mikael Magnusson
2 siblings, 1 reply; 5+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-10-21 21:06 UTC (permalink / raw)
To: Martijn Dekker, zsh-users
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Checking if a variable is exported
2016-10-21 20:27 Checking if a variable is exported Martijn Dekker
2016-10-21 21:00 ` Eric Cook
2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-10-21 21:12 ` Mikael Magnusson
2 siblings, 0 replies; 5+ messages in thread
From: Mikael Magnusson @ 2016-10-21 21:12 UTC (permalink / raw)
To: Martijn Dekker; +Cc: Zsh Users
On Fri, Oct 21, 2016 at 10:27 PM, Martijn Dekker <martijn@inlv.org> wrote:
> 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.
${(t)param} will contain "export".
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Checking if a variable is exported
2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-10-22 5:57 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2016-10-22 5:57 UTC (permalink / raw)
To: zsh-users
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-10-22 6:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-21 20:27 Checking if a variable is exported Martijn Dekker
2016-10-21 21:00 ` Eric Cook
2016-10-21 21:06 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-10-22 5:57 ` Bart Schaefer
2016-10-21 21:12 ` Mikael Magnusson
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).