bug-bash
[Top][All Lists]
Advanced

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

2.0..devel: `set -u; a=1; echo ${#a[@]}' fails


From: Koichi Murase
Subject: 2.0..devel: `set -u; a=1; echo ${#a[@]}' fails
Date: Sun, 6 Apr 2025 06:37:57 +0900

With nounset, ${#a[@]} for a scalar variable fails even though we can
correctly obtain the result (which is consistent with the expansion of
"${a[@]}") without nounset.  This is the result with the devel
version:

  $ bash -c 'a=1; echo ${#a[@]}'
  1
  $ bash -uc 'a=1; echo ${#a[@]}'
  bash: line 1: a: unbound variable

This behavior seems to be present from the very beginning
implementation of arrays in bash-2.0:

  $ bash-2.0 -c 'a=1; echo ${#a[@]}'
  1
  $ bash-2.0 -uc 'a=1; echo ${#a[@]}'
  bash-2.0: a: unbound variable

However, this behavior does not match that of other shells:

  $ ksh -c 'a=1; echo ${#a[@]}'
  1
  $ ksh -uc 'a=1; echo ${#a[@]}'
  1

  $ mksh -c 'a=1; echo ${#a[@]}'
  1
  $ mksh -uc 'a=1; echo ${#a[@]}'
  1

  $ zsh -c 'a=1; echo ${#a[@]}'
  1
  $ zsh -uc 'a=1; echo ${#a[@]}'
  1

Is this intentional?  If so, how can I understand the mental model or
the design background?  I'd be happy if the current behavior is fixed.

--
Koichi



reply via email to

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