I have a text file (or pipe output, doesn't matter here)memcached.uptime 1061374memcached.curr_connections 480memcached.cmd_get 478962548memcached.cmd_set 17641364memcached.cmd_flush 0If I use command cat test.txt | while read i; do echo $i; done it produces quite expected output:memcached.uptime 1061374memcached.curr_connections 480memcached.cmd_get 478962548etcBut if I loop over using for i in $(cat test.txt); do echo $i; done I see something different:memcached.uptime1061374memcached.curr_connections480memcached.cmd_get478962548etcThe question is: WHY???
new lines and bash variable
bash;variable;newlines;loop device
The answer is that $(command) expands to the raw output of the command, which your shell will then perform its usual word separation on.  Said separation consists of any whitespace being considered a word separator.You are also doing two different things with the text; in one you are parsing it through read (which works on line input and not word input, and in the other, you are iterating over the output of $(cat) in a for loop.  You could probably get similar results with IFS='\n' for i in $(cat test.txt); do echo $i; done.