Delimiter issue in bash for loop with cat command

If we had a list file which listing like:

aa
bb
cc dd

which we write a bash script and want to print out each line with cat command. The script listing as:

1
2
3
4
5
6
#!/bin/bash

for line in `cat list_file`
do
    echo $line
done

But the result is:

aa
bb
cc
dd

This issue is due to the Internal Field Separator (IFS) in bash is default defined as <space><tab><newline>. So the solution is simple. Just add the following command in bash script (line 2 of the above example) to overwrite it.

1
IFS=$'\n'

And the output result is correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *