zsh-users
 help / color / mirror / code / Atom feed
* Test if parameter is set
@ 2011-04-19 16:48 Thorsten Kampe
  2011-04-19 17:19 ` Frank Terbeck
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Thorsten Kampe @ 2011-04-19 16:48 UTC (permalink / raw)
  To: zsh-users

Hi,

how can I test if a parameter is set?

Like in...

if $VAR_IS_SET; then
    print Variable is set
else
    print Variable is not set
fi

Thorsten


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Test if parameter is set
  2011-04-19 16:48 Test if parameter is set Thorsten Kampe
@ 2011-04-19 17:19 ` Frank Terbeck
  2011-04-19 17:22 ` Stephane Chazelas
  2011-04-19 17:26 ` Peter Stephenson
  2 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-04-19 17:19 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: zsh-users

Thorsten Kampe wrote:
> how can I test if a parameter is set?
>
> Like in...
>
> if $VAR_IS_SET; then
>     print Variable is set
> else
>     print Variable is not set
> fi

In zsh:

if (( ${+parameters[foo]} )); then
   : set
else
   : unset
fi


In POSIX shells:

if [ x${foo+set} = xset ]; then
   : set
else
   : unset
fi

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Test if parameter is set
  2011-04-19 16:48 Test if parameter is set Thorsten Kampe
  2011-04-19 17:19 ` Frank Terbeck
@ 2011-04-19 17:22 ` Stephane Chazelas
  2011-04-19 17:26 ` Peter Stephenson
  2 siblings, 0 replies; 5+ messages in thread
From: Stephane Chazelas @ 2011-04-19 17:22 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: zsh-users

2011-04-19 18:48:57 +0200, Thorsten Kampe:
> Hi,
> 
> how can I test if a parameter is set?
> 
> Like in...
> 
> if $VAR_IS_SET; then
>     print Variable is set
> else
>     print Variable is not set
> fi
[...]

if (($+VAR))

or portably:

if [ -n "${VAR+x}" ]

-- 
Stephane


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Test if parameter is set
  2011-04-19 16:48 Test if parameter is set Thorsten Kampe
  2011-04-19 17:19 ` Frank Terbeck
  2011-04-19 17:22 ` Stephane Chazelas
@ 2011-04-19 17:26 ` Peter Stephenson
  2011-04-19 17:32   ` Peter Stephenson
  2 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2011-04-19 17:26 UTC (permalink / raw)
  To: zsh-users

On Tue, 19 Apr 2011 18:48:57 +0200
Thorsten Kampe <thorsten@thorstenkampe.de> wrote:
> how can I test if a parameter is set?

if (( ${+VAR} )); then
   ...

is the zsh-specific answer.  However, you can do this in a more standard
way, should you wish, with

if [[ -n ${VAR+1} ]]; then
   ...

(note no ":").

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Test if parameter is set
  2011-04-19 17:26 ` Peter Stephenson
@ 2011-04-19 17:32   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2011-04-19 17:32 UTC (permalink / raw)
  To: zsh-users

On Tue, 19 Apr 2011 18:26:41 +0100
Peter Stephenson <Peter.Stephenson@csr.com> wrote:
> However, you can do this in a more standard way, should you wish,
> with
> 
> if [[ -n ${VAR+1} ]]; then
>    ...

(Not yet *that* standard, in fact, but Frank has covered that.)

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio
Limited Churchill House, Cambridge Business Park, Cowley Road,
Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-04-19 17:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-19 16:48 Test if parameter is set Thorsten Kampe
2011-04-19 17:19 ` Frank Terbeck
2011-04-19 17:22 ` Stephane Chazelas
2011-04-19 17:26 ` Peter Stephenson
2011-04-19 17:32   ` Peter Stephenson

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).