[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
man page confusion about array with no members: var=()
From: |
idallen |
Subject: |
man page confusion about array with no members: var=() |
Date: |
Wed, 13 Jul 2016 16:36:00 -0400 (EDT) |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL
-DHAVE_CONFIG_H -I. -I../. -I.././include -I.././lib -Wdate-time
-D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat
-Werror=format-security -Wall
uname output: Linux idallen-oak 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24
10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 4.3
Patch Level: 46
Release Status: release
Description:
The BASH man pags says "When there are no array members,
${name[@]} expands to nothing." and then later "An array variable
is considered set if a subscript has been assigned a value.".
The first sentence tells me that ${name[@]} is a valid use of a
variable that exists but has no members, but the second sentence
implies that if the array has no members it is considered "unset"
and thus ${name[@]} would not be a valid use of the name.
These are contradictory statements. Using ${name[@]} with
no array members generates an "unbound variable" error under
"nounset"; it does not expand to "nothing".
The reason ${name[@]} expands to nothing is not because it's
a valid use of ${name[@]}, it's because any use of an unset
variable expands to nothing, unless, as I do, you run with
"nounset" enabled and it causes an error.
To fit current behaviour, the first man page sentence above should
be changed to say: When there are no array members, the array
name is considered unset and ${name[@]} will expand to nothing or
generate an "unbound variable" error under the "nounset" option.
I'd prefer that BASH change the behaviour of var=() to not give
an "unbound variable" error on ${name[@]}, but changing the man
page is probably easier than changing the current behaviour.
Repeat-By:
#!/bin/bash -u
# make BASH complain about unset variables
set -o nounset
echo 'ONE: set var= and try $var and ${var[@]} - both work without
error'
unset var
var=
( echo "ONE: [$var]" )
( echo "ONE: [${var[@]}]" )
echo 'TWO: set var=() and try again - both fail with "unbound variable"'
unset var
var=()
( echo "TWO: [$var]" )
( echo "TWO: [${var[@]}]" )
- man page confusion about array with no members: var=(),
idallen <=