Skip to main content

Bash loop with End of Line as delimiter

Here is how you do a for loop that uses the end of the line as the delimiter instead of space delimited.
The reason this is better is that you can set variable inside the loop and check them outside unlike doing it with a while read loop.
The testfile.data file can now have spaces on each line.

#!/bin/sh

IFS="
"

for i in `cat testfile.dat`
do
        CHECK="test"
        echo $i
done
echo ${CHECK}