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.

I'm learning bash scripting right now.

I was reading this thread: How to loop over the lines of a file?

What is IFS?

With for and IFS:

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

IFS stands for Input Internal Field Separator - it's a character that separate fields. In the example you posted it is set to new line character (\n), so after setting it for will process text line by line. In that example you could change value of $IFS (to some letter that you have in your input file) and check how text will be splitted.

BTW, from the answer you posted second solution is that recommended...

As @jasonwryan noticed it's not Input but Internal. Name Input came from awk in which there is also OFS - Output Field Separator.

share|improve this answer
    
Ah! Thanks that makes more sense. –  chrisjlee Jul 6 '11 at 20:52
add comment

IFS isn't directly related to looping, it's related to word splitting. IFS indirectly determines how the output from the command is broken up into pieces that the loop iterates over.

When you have an unprotected variable substitution $foo or command substitution $(foo), there are two cases:

  • If the context expects a single word, e.g. when the substitution is between double quotes "$foo", or in a variable assignment x=$foo, then the string resulting from the substitution is used as-is.
  • If the context expects multiple words, which is the case most of the times, then two further expansions are performed on the resulting string:
    • The string is split into words. Any character that appears in $IFS is considered a word separator. For example IFS=":"; foo="12:34::78"; echo $foo prints 12 34 ​ 78 (with two spaces between 34 and 78, since there's an empty word).
    • Each word is treated as a glob pattern and expanded into a list of file names. For example, foo="*"; echo $foo prints the list of files in the current directory.

For loops, like many other contexts, expect a list of words. So

for x in $(foo); do …

breaks $(foo) into words, and treats each word as a glob pattern. The default value of IFS is space, tab and newline, so if foo prints out two lines hello world and howdy then the loop body is executed with x=hello, then x=world and x=howdy. If IFS is explicitly changed to contain a newline only, then the loop is executed for hello world and howdy. If IFS is changed to be o, then the loop is executed for hell, ​ w, rld​␤h (where ​␤ is a newline character) and wdy.

share|improve this answer
add comment

From man bash

IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is "<space><tab><newline>".

This is one of Bash's internal variables. It determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

While it defaults to whitespace (space, tab, and newline), it may be changed, for example, to parse a comma-separated data file.

http://tldp.org/LDP/abs/html/internalvariables.html

share|improve this answer
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.