helpThe GNU Bourne-Again SHell - Support: sr #109562, Using ${#a[@]} with an array...

 
 

sr #109562: Using ${#a[@]} with an array without any assigned elements when the nounset option is enabled throws

Submitted by:  None
Submitted on:  Mon 10 Sep 2018 11:28:13 PM UTC  
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Works For Me
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Wed 12 Sep 2018 07:43:30 PM UTC, comment #1: 

Yes, the variable really is unset. It's unset unless it's been assigned a value, which in the case of arrays means at least one assigned element. The documentation says as much.

Bash-4.2 was inconsistent here. Bash-4.3 changed that to consistently treat both ${#a[@]} and ${a[@]} as unset if there were no assigned elements.

The POSIX group released an interpretation that said that $@ should not throw an error when nounset is enabled and there are no positional parameters, so bash-4.4 changed to honor that in the case of ${a[@]}, since ${a[@]} and $@ are supposed to parallel each other as much as possible.

That was the result of

http://lists.gnu.org/archive/html/bug-bash/2016-07/msg00025.html

There's no similar analog between ${#a[@]} and $#, since the latter is a special parameter, not a length operator, so I left that alone.

Chet Ramey <chet>
Project Administrator
Mon 10 Sep 2018 11:28:13 PM UTC, original submission:  

This behavior is different with previous versions of bash:

In 4.4:

4.4.23# set +u
4.4.23# unset a
4.4.23# echo $BASH_VERSION
4.4.23(1)-release
4.4.23# declare -A a
4.4.23# echo "${#a[@]}"
0
4.4.23# set -u
4.4.23# echo "${#a[@]}"
-bash: a: unbound variable
4.4.23# echo "${!a[@]}"

4.4.23# echo "${a[@]}"

4.4.23# declare -A a=()
4.4.23# echo "${#a[@]}"
0

In 4.2:

4.2.46# set +u
4.2.46# unset a
4.2.46# echo $BASH_VERSION
4.2.46(1)-release
4.2.46# declare -A a
4.2.46# echo "${#a[@]}"
0
4.2.46# set -u
4.2.46# echo "${#a[@]}"
0
4.2.46# echo "${!a[@]}"

4.2.46# echo "${a[@]}"
-bash: a[@]: unbound variable
4.2.46# declare -A a=()
4.2.46# echo "${#a[@]}"
0

Before, the =() was not necessary. Also, ${!a[@]} and ${a[@]} both do not throw, when ${#a[@]} does.
But to be fair, ${a[@]} used to throw and it no longer does.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

     

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-09-12 chet StatusNone => Works For Me

    Back to the top


    Powered by Savane 3.5