Consider this bash session:
set -- 1 2 3
echo "$*" # 1 2 3 (as expected)
IFS=a echo "$*" # 1 2 3 (why not "1a2a3"?)
IFS=a; echo "$*" # 1a2a3a (as expected)
Why does the "assignment before command" syntax not work to set IFS
to a different value for the duration of the single command? It appears you must change the value of IFS
first with a separate command.
Because IFS
affects the parsing (tokenization, word-splitting) of a command, so it must be set before that command is parsed. This is why IFS=a echo "$*"
can only use the original IFS
, not a
.
It's a somewhat similar case to FOO=bar echo $FOO
not echoing bar
. $FOO
is substituted (empty) and then the command, with it's variable assignment is executed.