For loop with spaces

If you want to loop over file or data that have spaces in them, you must change the shell's delimiter.

IFS=$(echo -en "\n\b")

For example:

$ for X in `cat list.txt`; do echo $X; done

Agatha
Christie/Death
on
the
Nile
(1184)/Death
On
The
Nile
-
Agatha
Christie.lit
Agatha
Christie/Passenger
to
Frankfurt
(44)/PASSENGER
TO
FRANKFURT

$ IFS=$(echo -en "\n\b")
$ for X in `cat list.txt`; do echo $X; done

Agatha Christie/Death on the Nile (1184)/Death On The Nile - Agatha Christie.lit
Agatha Christie/Passenger to Frankfurt (44)/PASSENGER TO FRANKFURT - Agatha Christie.lit
Comments