Sometimes when writing script it can be necessary to set IFS
(the internal field separator) to something other than the default, e.g. to change the way $*
will be expanded.
While this is totally plausible I can't think of any reason to set IFS
in interactive shells to anything but the default (which is space, tab and newline if I remember correctly). Still I was often told that on an interactive shell I can't expect IFS
to be set to it's default value.
Is this really true? Can anyone give practical examples?
Best Answer
In an interactive shell,
$IFS
will be the default unless you changed it or something in shell startup scripts (like~/.bashrc
forbash
) or any other scripts sourced or shell functions executed since the shell was started change it (they generally wouldn't or would change it back to the default if they are properly written though).If those functions defined in rc files or other sourced files are properly written and they need to use word splitting, they would not assume anything on the current value of IFS, so it should be fine for you do set it to a different value.
However, once you change it, you have to remember that you did as that might affect the way command lines you run later are interpreted.
For instance, if you do:
To list the directories in
$PATH
, you might wonder why later on things like:do not work any more. So you might as well write it:
Or:
(BTW,
unset IFS
restores the word splitting to its default behaviour, but not$IFS
and it breaks the (not properly written) functions that do things likeoldIFS=$IFS; IFS=xxx; ...; IFS=$oldIFS
to restore$IFS
).