I have a text file (or pipe output, doesn't matter here)
memcached.uptime 1061374
memcached.curr_connections 480
memcached.cmd_get 478962548
memcached.cmd_set 17641364
memcached.cmd_flush 0
If I use command cat test.txt | while read i; do echo $i; done
it produces quite expected output:
memcached.uptime 1061374
memcached.curr_connections 480
memcached.cmd_get 478962548
etc
But if I loop over using for i in $(cat test.txt); do echo $i; done
I see something different:
memcached.uptime
1061374
memcached.curr_connections
480
memcached.cmd_get
478962548
etc
The question is: WHY???
for
loop iterates over individual whitespace-separated words, and awhile-read
loop iterates over lines. See http://mywiki.wooledge.org/BashFAQ/001 and http://mywiki.wooledge.org/DontReadLinesWithFor – glenn jackman Dec 07 '16 at 21:34