I have a text.txt file like this
line1
line2
line3
I want to write a script that loops over each line and echo out
modified line1
modified line2
modified line3
This is the script which is a very common solution:
while IFS= read -r line; do
echo modified $line
done <<< $(cat ~/text.txt)
But the output I got was:
modified line1 line2 line3
What went wrong?
<<< $(cat ...)command substitution + here string is completely unnecessary and messing things up, but I guess that you're using some older (< 4.0?) version ofbash, which will ifs-split+glob the word following<<<.ifs-split+glob the word following <<<?IFSvariable and then glob-expand the resulting words (eg.a='* .*'; echo $a). This should not happen on a variable used as a here-string (eg.a='* .*'; cat <<<$ashould print just* .*), but it does happen on some older buggy versions ofbash.