On my GNU bash, version 4.3.42(1)-release
I am doing some tests to answer a question. The idea is to split a :
-separated string and and each of its elements into an array.
For this, I try to set the IFS
to :
in the scope of the command, so that the split is automatic and IFS
remains untouched:
$ myvar="a:b:c"
$ IFS=: d=($myvar)
$ printf "%s\n" ${d[@]}
a
b
c
And apparently IFS
remains the same:
$ echo $IFS # empty
The BASH reference says that:
If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
However, then I notice that the IFS
is kind of broken, so that echo $myvar
returns a b c
instead of a:b:c
.
Unsetting the value solves it:
$ unset IFS
$ echo $myvar
a:b:c
But I wonder: what is causing this? Isn't IFS=: command
changing IFS
just in the scope of the command being executed?
I see in Setting IFS for a single statement that this indeed works:
$ IFS=: eval 'd=($myvar)'
$ echo $myvar
a:b:c
But I don't understand why it does and IFS=: d=($myvar)
does not.
echo $IFS
andecho "$IFS"
. As everybody always says, quote your variables in bash. :-) – Procurator