${subscriptref} triggers nounset, but $subscriptref does not

2025-05-14 Thread Emanuele Torre
For example:

$ # a[0] is not set, but no error
$ bash -uc 'a=() k=; declare -n r=a[k]; : "$r"; echo ok'
ok
$ # a[0] is not set, error
$ bash -uc 'a=() k=; declare -n r=a[k]; : "${r}"; echo ok'
bash: line 1: r: unbound variable

Can reproduce in bash 5.2.37, and devel branch.

Bash modifies the parameter_expand flags before calling
parameter_brace_expand_word() when using ${r} in this case,

subst.c:10072-10075 (devel 535a8150b65ee6888f54f602274dbbdcd77c788e)
else
  {
local_pflags |= PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS));
tdesc = parameter_brace_expand_word (name, var_is_special, quoted, 
local_pflags, &es);

but it does not do that before calling it when using $r

subst.c:10973-10976 (devel 535a8150b65ee6888f54f602274dbbdcd77c788e)

if (temp && *temp && valid_array_reference (temp, 0))
  {
chk_atstar (temp, quoted, pflags, quoted_dollar_at_p, 
contains_dollar_at);
tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), 
quoted, pflags, 0);

Maybe that is related to why this is happening.

o/
 emanuele6



Re: ${subscriptref} triggers nounset, but $subscriptref does not

2025-05-14 Thread Emanuele Torre
On Thu, May 15, 2025 at 02:26:47AM +0200, Emanuele Torre wrote:
> $ # a[0] is not set, but no error
> $ bash -uc 'a=() k=; declare -n r=a[k]; : "$r"; echo ok'
> ok
> $ # a[0] is not set, error
> $ bash -uc 'a=() k=; declare -n r=a[k]; : "${r}"; echo ok'
> bash: line 1: r: unbound variable

You don't need the k variable nor a to be set:

$ bash -uc 'unset -v a; declare -n r=a[0]; : "${r}"; echo ok'
bash: line 1: r: unbound variable
$ bash -uc 'unset -v a; declare -n r=a[0]; : "$r"; echo ok'
ok

o/
 emanuele6