${!prefix@} When ‘@’ is used and the expansion appears within double quotes

Viewed 148

I don't understand something the bash manual states about prefix parameter expansions.

The bash manual says this:

${!prefix*}
${!prefix@}

Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable. When ‘@’ is used and the expansion appears within double quotes, each variable name expands to a separate word.

When I execute this in a bash:

aaa=1
aab=2
aac=3
echo "${!aa@}"

The resulting output is this: aaa aab aac

That works as expected. However, the thing is that I get the same output when I use echo "${!aa*}", and I don't understand the difference.

1 Answers

echo joins all its arguments by a space and prints the result with a newline at the end. Using printf instead, you can see the difference clearly.

$ aaa=1
$ aab=2
$ aac=3
$ printf '<%s>\n' "${!aa@}"
<aaa>
<aab>
<aac>
$ printf '<%s>\n' "${!aa*}"
<aaa aab aac>

Besides, while words are joined by the first character of IFS in the expansion of *; IFS is not honored at all when @ is used.

$ IFS=,
$ echo "${!aa@}"
aaa aab aac
$ echo "${!aa*}"
aaa,aab,aac
Related