bug-bash
[Top][All Lists]
Advanced

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

What is correct syntax to check for empty array? behavior diffs between


From: L A Walsh
Subject: What is correct syntax to check for empty array? behavior diffs between 4.3 and 4.4
Date: Thu, 26 Jan 2017 13:11:25 -0800
User-agent: Thunderbird

I noticed an error coming out of a script when I tried
to check an array's size w/unbound var checking enabled

(note my=declare, array='my -a')

First w/bash-4.3.42:

 bash                        # try in subshell in case '-u' exits
 array gv_ops=()
 echo ${#gv_ops[@]}          # existing var
0
 echo ${#gv_ops2[@]:-0}      # non-existing var (seems to work)
 0
 set -u                      # now add an undefined check
 echo ${#gv_ops[@]:-0}       # empty array still shows '0'
0
 echo ${#gv_ops2[@]:-0}      # try w/non-existing array
-bash: gv_ops2: unbound variable  # doesn't work
----

w/bash 4.4:

 array gv_ops=()
 echo ${#gv_ops[@]:-0}
bash: ${#gv_ops[@]:-0}: bad substitution
 echo ${#gv_ops2[@]:-0}
bash: ${#gv_ops2[@]:-0}: bad substitution

ok, so 4.4 is telling me its a bad substitution while 4.3 hid
that, but fails when the array doesn't exist.  So I see that
it doesn't work in 4.3 or 4.4... so what does?

I.e. - How do I display catch an undefined array and have it
display a known value?

Thanks!
-linda






reply via email to

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