Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15723
| From | Martin Schulte <gnu@schrader-schulte.de> |
|---|---|
| Newsgroups | gnu.bash.bug |
| Subject | Re: Two states of empty arrays |
| Date | 2019-12-12 22:34 +0100 |
| Message-ID | <mailman.751.1576186508.1979.bug-bash@gnu.org> (permalink) |
| References | <c9224e23-ff7d-ba84-dc4f-aa68db902f72@noiraude.net> <79b6365d-4c09-7122-25c3-4564fd304948@case.edu> <mailman.740.1576178010.1979.bug-bash@gnu.org> <qsua0m$fkh$1@solo.fdn.fr> <20191212223452.157114bb8c51cb2689b793b8@schrader-schulte.de> |
Hello Léa!
Léa Gris <lea.gris@noiraude.net> wrote:
> I was trying to play the the -v test to detect when an array or
> associative array has been declared, not necessarily assigned entries
> key, values, to not error when Bash runs with -o nounset
Just for the curious: What is your attention here?
I think that most useful questions (Is there an element in the array? Is
there a value for a given key?) can be answered in a simpler way:
#!/bin/bash
set -o nounset
# From what I learned today it seems to be good practice to always
# assign and empty array to when declaring an associative array:
declare -A assoc=()
echo ${#assoc[@]} # Are there elements in it?
assoc[key1]=val1
assoc[key2]=
for key in key1 key2 key3; do
if [[ -n ${assoc[$key]+isset} ]]; then
echo "Element for $key is set"
else
echo "No Element for $key"
fi
done
Best regards,
Martin
Back to gnu.bash.bug | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: Two states of empty arrays Chet Ramey <chet.ramey@case.edu> - 2019-12-12 14:13 -0500 Re: Two states of empty arrays Léa Gris <lea.gris@noiraude.net> - 2019-12-12 22:04 +0100 Re: Two states of empty arrays Martin Schulte <gnu@schrader-schulte.de> - 2019-12-12 22:34 +0100
csiph-web