[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Expansion of unquoted special parameter $@ when used as the word for
From: |
Robin A. Meade |
Subject: |
Re: Expansion of unquoted special parameter $@ when used as the word for a HERE STRING |
Date: |
Tue, 5 Nov 2019 12:15:22 -1000 |
OK, thanks. I'll avoid unquoted $* and $@ in my scripts.
However, I was testing the validity of this sentence:
"In contexts where word splitting is not performed, this expands to a
single word with each positional parameter separated by a space."
– https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
That statement appears to be true for variable assignments:
set -- a 'b c'
v=$@
# ^ word splitting does not occur in this context
echo "$v"
Prints:
a b c
But not for here strings:
cat <<< $@;
# ^ again a context in which word splitting does not occur
Prints:
a b c
On Tue, Nov 5, 2019 at 11:23 AM Greg Wooledge <address@hidden> wrote:
> On Tue, Nov 05, 2019 at 10:30:39AM -1000, Robin A. Meade wrote:
> > Consider:
> >
> > set a 'b c'
> > cat <<< $@;
> >
> > The output is:
> >
> > a b c
> >
> > I expected the 3 spaces between b and c to be preserved.
>
> wooledg:~$ set -- a 'b c'
> wooledg:~$ cat <<< "$*"
> a b c
> wooledg:~$ cat <<< "$@"
> a b c
> wooledg:~$ cat <<< $@
> a b c
>
> Technically only "$*" is correct here. That expands to a single word.
> "$@" expands to a list of words, and it doesn't really make sense to
> use a here-string with a list of strings.
>
> Unquoted $* and $@ are just plain wrong, and you shouldn't use them.
>