Unix/Linux — Bash — String seperator

by

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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: