Monday, June 06, 2005

Wat is IFS?

IFS is Internal Field Separators (normally space, tab and newline). The cut -d option temporarily changes the IFS for the duration of the cut command.
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:
$
$ test=I_AM_A_GOOD_BOY
$ for i in $test
> do
> echo $i
> done
I_AM_A_GOOD_BOY
$
$
$ OIFS=$IFS
$ IFS=_
$ for i in $test
> do
> echo $i
> done
I
AM
A
GOOD
BOY
$
$IFS=$OIFS


The first command assigns the string “I_AM_A_GOOD_BOY” to the variable named test. 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 new line character.

After redefining IFS, the second for loop treats the string as four separated fields, each delimited by an underscore.
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
0000004
$

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.

Labels:

0 Comments:

Post a Comment

<< Home