In bash on Mac OSX, I have a file (test case) that contains the lines
x xx xxx
xxx xx x
But when I execute the command
for i in `cat r.txt`; do echo "$i"; done
the result is not
x xx xxx
xxx xx x
as I want but rather
x
xx
xxx
xxx
xx
x
How do I make echo give me 'x xx xxx'?
By default, a Bash for loop splits on all whitespace. You can override that by setting the IFS
variable:
IFS=$'\n'
for i in `cat r.txt`; do echo "$i"; done
unset IFS
Either setting IFS
as suggested or use while
:
while read theline; do echo "$theline"; done <thefile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With