[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: parameter expansion null check fails for arrays when [*] or [@] is u
From: |
Ilkka Virta |
Subject: |
Re: parameter expansion null check fails for arrays when [*] or [@] is used |
Date: |
Wed, 23 Mar 2022 09:25:43 +0200 |
On Tue, Mar 22, 2022 at 11:52 PM L A Walsh <bash@tlinx.org> wrote:
> On 2022/03/21 05:45, Andreas Luik wrote:
> > Description:
> > Bash fails to correctly test for parameter to be unset or null
> when the parameter is an array reference [*] or [@].
> >
> > Repeat-By:
> >
> > myvar[0]=
> > echo "${myvar[0]:+nonnull}"
> > <empty> -> OK
> > echo "${myvar[*]:+nonnull}"
> > nunnull -> not OK, because "${myvar[*]}" is null
> >
> myvar[*] = ('', ) element 0 contains an empty string, so not null.
>
The POSIX phraseology is that "null" means the empty string.
${var:+text} tests if var is unset or null, ${var+text} only tests if it's
unset.
https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#tag_18_06_02
"${myvar[*]}" produces the empty string, same as "${myvar[0]}", so it is
a bit odd if using the :+ modifier on them gives different behaviour.
Compare e.g.
a="${myvar[*]}"
echo "${a:+nonnull}"
# vs.
echo "${myvar[*]:+nonnull}"
Not sure what "${array[@]:+text}" should even do though. With @, it should
result in one word per element, but how that should combine with :+ I have
no idea.
It doesn't matter, though, Bash 5.0 looks to give the empty output for all
of those.
Re: parameter expansion null check fails for arrays when [*] or [@] is used, Chet Ramey, 2022/03/23