Method 1 - Using "For"
To use for
loop to read a file, the IFS
(Internal Field Separator) must be set to the newline character.
#!/bin/bash
FILE="myfile.txt"
IFS=$'\n'
for LINE in $(cat "$FILE"); do
# Here you can process each line
echo "$LINE"
done
# Reset 'IFS' to its default value
IFS=' '
Method 2 - Using read
I usually use method 1 but maybe this is simpler and more readable
#!/bin/bash
FILE="myfile.txt"
while IFS= read -r LINE; do
# Here you can process each line
echo "$LINE"
done < $FILE