Package: bash
Version: 3.2-4
Severity: normal

For a reference to the whole array held in an array variable (e.g.,
"${v...@]}"), Bash's nounset option ("set -o nounset") treats a
reference to a variable bound to an empty array as if the variable were
unset (unbound).


That behavior differs from the behavior that the manual page seemingly
specifies or documents:

The description of the "nounset" / "-u" option says:

   Treat unset variables as an error when performing parameter
   expansion.  If expansion is attempted on an unset variable, the
   shell prints an error message, and, if not interactive, exits
   with a non-zero status.

Nothing there says that variables that are set to arrays that are
empty are treated as unset variables, and there doesn't seem to be
any other part the manual page that says that.


If by chance the current behavior is intentional (and the manual page
is considered to be the thing that is in error), then take this report
as a bug report about that design intention, and note how the current
behavior is inconvenient and not orthogonal (not keeping variable
presence and array size separate).

Although a simple case such as this:

    possibly_empty_array=( ... )

    for i in "${possibly_empty_arr...@}}" ; do
        ...
    done

one can relatively easily wrap an if statement around the loop,
in cases such as this:


    never_empty=( ... )
    maybe_empty=( ... )
    maybe_empty2=( ... )

    somecommand "${never_emp...@}}" "${maybe_emp...@}}" "${maybe_empt...@}}"

working around the irregularity is much more disruptive.


Included below is a script that demostrates the current behavior.  The
output that I see is:

$ ./testNounsetEmptyArray.sh
Test: unset:
.../testNounsetEmptyArray.sh: line 6: arr...@]: unbound variable
UNSET
Test: set, non-empty:
SET
Test: set, empty:
.../testNounsetEmptyArray.sh: line 6: arr...@]: unbound variable
UNSET
$


-- System Information:
Debian Release: 5.0.1
  APT prefers stable
  APT policy: (500, 'stable')
Architecture: i386 (i686)

Kernel: Linux 2.6.26-2-686 (SMP w/1 CPU core)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages bash depends on:
ii  base-files                5lenny2        Debian base system miscellaneous f
ii  debianutils               2.30           Miscellaneous utilities specific t
ii  libc6                     2.7-18         GNU C Library: Shared libraries
ii  libncurses5               5.7+20081213-1 shared libraries for terminal hand

Versions of packages bash recommends:
ii  bash-completion               20080705   programmable completion for the ba

Versions of packages bash suggests:
pn  bash-doc                      <none>     (no description available)

-- no debconf information

*** testNounsetEmptyArray.sh
#!/bin/sh

set -o nounset

function test ()
{
    trap "echo UNSET" EXIT

    echo "Test: $1: "
    for i in "${arr...@]}" ; do : ; done

    echo "SET"

    trap - EXIT                 # ca
}

( unset array;   test "unset"          )
( array=( a b ); test "set, non-empty" )
( array=( );     test "set, empty"     )





--
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to