Pages

Why save $IFS to OIFS ?

A lot of people seem to get confused just because $IFS contains non printing characters. Many suggest that before changing IFS (while using a modern shell) it should be saved in a variable such as OIFS. Others suggest going into a subshell so the original IFS won'be changed. Looking at IFS as it is by default on my system it's just space tab and newline

~$ echo -En "$IFS"|hexdump -bc
0000000 040 011 012
0000000 \t \n
0000003

So if you know what IFS you need just set it to that. In bash, zsh, ksh93 the following works fine. IFS=$'\040\t\n' Do any IFS changing you want and then set it to that.

~$ string=a/b/c
~$ IFS='/'
~$ set -- $string
~$ IFS=$'\040\t\n'
~$ echo $1,$2,$3
a,b,c

No comments: