Archive for November, 2006

Unix/Linux — Bash — String seperator

November 6, 2006

In Bash the default string separator is whitespace which means spaces, tabs, or blank lines. The string separator is used in various places, including when looping with for over a string. The string separator is stored in the IFS variable.

If you want to set the default separator to “:” you need to do:

IFS=:

If you want to set is to newline, you can do it like this:

IFS=$’\x0a’

‘\x0a’ is the hexadecimal notation for newline or Ctrl-J.

A common pattern is to save the old separator before changing and restore it when you are done:

IFS_OLD=$IFS
IFS=:

IFS=$IFS_OLD

or more simple, just unset it when you are done:

IFS=:

unset IFS