UNIX Tutorials, Tips, Tricks and Shell Scripts

Useful Shell Script Variables - Part III - IFS (Internal Field Separator)

Another shell script variable that you should become comfortable using is the IFS, or internal field separator, variable. The shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.

IFS can be redefined to parse one or more lines of data whose fields are not delimited by the default white-space characters. Consider this sequence of variable assignments and for loops:

$ line=learn:unix:at:livefire:labs
$ for i in $line
> do
> echo $i
> done
$ IFS=:
$ for i in $line
> do
> echo $i
> done

The first command assigns the string .learn:unix:at:livefire:labs. to the variable named line. You can see from the first for loop that the shell treats the entire string as a single field. This is because the string does not contain a space, tab, or newline character.

After redefining IFS, the second for loop treats the string as four separated fields, each delimited by a colon. Using a colon for IFS would be appropriate when parsing the fields in a record from /etc/passwd, the user account information file:


Notice that the original value of IFS was stored in OIFS (.O. for original) prior to changing its value. After you are finished using the new definition, it would be wise to return it to its original value to avoid unexpected side effects that may surface later on in your script.

TIP . The current value of IFS may be viewed using the following pipeline:

$ echo "$IFS" | od -b
0000000 040 011 012 012

The output of the echo command is piped into the octal dump command, giving you its octal equivalent. You can then use an ASCII table to determine what characters are stored in the variable. Hint: Ignore the first set of zeros and the second newline character (012), which was generated by echo.

Do you need to learn UNIX shell scripting and get practice writing & running scripts...on a REAL SERVER? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to a real server for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.

Thanks for reading and have a terrific day!!!

Has this article been helpful to you? Would it benefit others? If you answered "yes" to either question, kindly share the page.

Thank you for sharing!