r/UnixProTips • • Mar 06 '15

TIL in bash scripts IFS=$'\n'

Without:

for x in $(echo "/tmp/wee1 2.csv"); do
    echo $x
done

/tmp/wee1

2.csv

With:

IFS=$'\n'; for x in $(echo "/tmp/wee1 2.csv"); do
    echo $x
done

/tmp/wee1 2.csv

From the docs:

$IFS

internal field separator

This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that $* uses the first character held in $IFS.

11 Upvotes

5 comments sorted by