bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: test whether array variable is set with ${parameter:+word}


From: Eduardo A . Bustamante López
Subject: Re: test whether array variable is set with ${parameter:+word}
Date: Fri, 17 Apr 2015 08:52:45 -0500
User-agent: Mutt/1.5.23 (2014-03-12)

This is not a bug.

This is due to not understanding two aspects of bash:

1) $arrname is the same as ${arrname[0]} (not the "first" element, as what you
   seem to imply). Bash arrays are sparse, so you can define the element at
   index 1, without having an element at 0.
2) unset and empty is not the same. With :+ you're testing emptiness. With +
   you can test if a parameter is set.

If your aim is to know if a certain array is defined, use:


dualbus@yaqui ~ % bash -c 'a=(); declare -p -- b 2>/dev/null >&2; echo $?'
1
dualbus@yaqui ~ % bash -c 'a=(); declare -p -- a 2>/dev/null >&2; echo $?'
0
dualbus@yaqui ~ % bash -c 'a=(1); declare -p -- a 2>/dev/null >&2; echo $?'
0

-- 
Eduardo Bustamante
https://dualbus.me/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]