[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: man page confusion about array with no members: var=()
From: |
Chet Ramey |
Subject: |
Re: man page confusion about array with no members: var=() |
Date: |
Sun, 17 Jul 2016 12:45:07 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
On 7/13/16 4:36 PM, idallen@idallen-fibe.dyndns.org wrote:
> 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.
Thanks for the report. The closest analog of the behavior you describe
is the use of $@ and $* when there are no positional parameters. Posix
decided a while back that these would not constitute referencing an unset
variable, and I think it's reasonable to make ${var[@]} and ${var[*]}
behave similarly.
In bash-4.4 these uses will not generate an `unset variable' error.
> 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[@]}]" )
`var' isn't unset, and it's not an array. The man page says explicitly
that the null string is a valid value.
>
> echo 'TWO: set var=() and try again - both fail with "unbound variable"'
> unset var
> var=()
> ( echo "TWO: [$var]" )
> ( echo "TWO: [${var[@]}]" )
This is technically unset, but will no longer generate an error.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/