bug-bash
[Top][All Lists]
Advanced

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

Zero-length indexed arrays


From: Dale R. Worley
Subject: Zero-length indexed arrays
Date: Thu, 16 Dec 2021 23:01:27 -0500

A bit ago I was debugging a failing script at work.  It turns out that
when you say
    FOO=(x y z)
then the variable FOO is an array and is defined.  But when you say
    FOO=()
then the variable FOO is an array (because ${#FOO[*]} substitutes an
integer viz. 0) but it is *not* defined (because ${FOO[*]} generates an
error when "set -u" is in effect).

I really don't like this.  But it is according to the manual (because
FOO has no index that has a value), and no doubt there are scripts out
there that depend subtly on this case.

It turns out that this can matter, if you do something like this:

    set -u    # for safety
    ...
    if ...
    then
        FOO=(...)
    else
        FOO=()
    fi
    ...
    FOO_PLUS=("${FOO[@]}" x y z w)

Dale



reply via email to

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