[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bash 4.4.5] Variable indirection, error on empty variable
From: |
konsolebox |
Subject: |
Re: [Bash 4.4.5] Variable indirection, error on empty variable |
Date: |
Mon, 28 Nov 2016 22:59:41 +0800 |
On Mon, Nov 28, 2016 at 5:03 PM, Otenba <otenba@protonmail.com> wrote:
>>It is indeed an error to attempt to perform indirection on an empty or
>> unset parameter.
> What I showed was that it is an error only for an empty parameter. If it's
> unset then it will do what it did in Bash 4.3, which is return an empty
> string without error.
>
> And another possibility is if the indirect variable has a string that is not
> a variable. That, right now, also returns an empty string without error.
>
> $ indirect=
> $ [[ ${!indirect} == 1 || 1 == 1 ]] && echo yes
> -bash: : bad substitution
> $ indirect=notavariable
> $ [[ ${!indirect} == 1 || 1 == 1 ]] && echo yes
> yes
> $ unset indirect
> $ [[ ${!indirect} == 1 || 1 == 1 ]] && echo yes
> yes
>
> In my opinion, in all three cases variable indirection should errorlessly
> return an empty string.
This seems pretty much consistent since the indirection method expands
the variable name assigned to the variable. It just returns an empty
string (or does something like that) when the variable is unset, where
in such case, no variable name value is assigned. Note that an
invalid variable name like 1234a would also cause bad substitution.
You would be giving an exemption if you don't produce an error with
"", but do with "1234a".
It probably should be questioned why no error message is made when the
reference variable is unset, but for an error message to be shown when
the variable has an invalid variable name assigned (empty string or
not), it's fine and is the correct one.
--
konsolebox