Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
2  
Your question would be better if you explained the acronym IFS. –  N.N. Feb 27 '13 at 15:25
    
it is the internal field separator in this context. i agree he should have clarified. –  amphibient Feb 27 '13 at 16:20
    
@foampile Please include it in your question by editing it rather than putting it in a comment. Will be easier for others to spot then. –  N.N. Feb 27 '13 at 16:48
    
@N.N. -- the question was not mine. do check –  amphibient Feb 27 '13 at 16:49
    
but i did edit the OP, it is pending approval –  amphibient Feb 27 '13 at 16:50
show 2 more comments

2 Answers

up vote 7 down vote accepted

In an interactive shell, $IFS will be the default unless you changed it or something in shell startup scripts (like ~/.bashrc for bash) 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:

IFS=:; ls -- $PATH

To list the directories in $PATH, you might wonder why later on things like:

rm -f -- $(cat ~/my.file.list)

do not work any more. So you might as well write it:

(IFS=:; ls -- $PATH)

Or:

IFS=:; ls -- $PATH; unset IFS

(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 like oldIFS=$IFS; IFS=xxx; ...; IFS=$oldIFS to restore $IFS).

share|improve this answer
    
You can also set it for a single command IFS=: ls -- $PATH, without the subshell. –  jordanm Feb 27 '13 at 16:28
3  
@jordanm, No. that inserts IFS=: into the environment passed to ls (which has no use for it), but doesn't affect how that $PATH is split in this command line. You could do IFS=: command eval 'ls -- $PATH', but that's a bit cumbersome. –  Stephane Chazelas Feb 27 '13 at 16:35
    
+1 Very nice answer. –  helpermethod Feb 28 '13 at 12:44
add comment

Of course you can and should expect that IFS is set to default, as without it, all sorts of commands will simply stop working altogether.

You change IFS in a shell (temporarily) for the same reasons you do it in a bash script, when you happen to need it (which rarely ever happens). If you're going to use a specific command only once there is no reason to write it into a shell script. Other than that there is no difference between a shell script, and using the shell directly.

share|improve this answer
2  
To remove confusion, IFS is not exported to the environment, and even if it is shells, ignore it upon starting, so setting IFS is the interactive shells will not affect any command started by that shell, unless those commands are built in the shell like read or . (aka source) or the functions you define. –  Stephane Chazelas Feb 27 '13 at 16:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.