Bash – Internal Field Separator (IFS)

By default in Bash, the Internal Field Separator (IFS) is a ‘ ‘ character.

This is used to determine how bash splits text input into different fields. For example:

for file in *.jpg; do echo $file; done

will output each jpg filename on a separate line, unless there are spaces used in the filename.

For a more complete solution, change the IFS to the new line character and list each filename on a new line:

IFS=$'\n'
for file in `ls -1 *.jpg;` do echo "$file"; done

Of course the above doesn’t really achieve much, but it might be used as part of a simple script to resize images:

IFS=$'\n'
for file in `ls -1 *.jpg`; do convert "$file" -resize 200 "thumb-$file"; done