On my GNU bash, version 4.3.42(1)-release I do some tests to answer the question . The idea is to split a : -separated string and each of its elements into an array.
To do this, I try to set IFS in : in the scope of the command so that the split is automatic and the IFS remains untouched:
$ myvar="a:b:c" $ IFS=: d=($myvar) $ printf "%s\n" ${d[@]} a b c
And, obviously, IFS remains unchanged:
$ echo $ IFS
# empty
The BASH link says that:
If IFS is not specified, parameters are separated by spaces. If IFS is null, the parameters are joined without intermediate delimiters.
However, I noticed that IFS is incorrect, so echo $myvar returns abc instead of a:b:c .
Cancels the value:
$ unset IFS $ echo $myvar a:b:c
But I wonder: what causes this? Is IFS=: command change to IFS only in the scope of the command being executed?
I see in the IFS setup for one statement that this really works:
$ IFS=: eval 'd=($myvar)' $ echo $myvar a:b:c
But I donβt understand why he does it, and IFS=: d=($myvar) does not work.
source share