Take the tour ×  I'm trying to read a text file and do something
with each line, using a bash script.  So, I have a list that
looks like this:   I thought I could loop over this using a
while loop, like so:  while read server; do   ssh $server "uname
-a" done < /home/kenny/list_of_servers.txt  The while loop stops
after 1 run, thus only running uname -a on server1  However,
with a for loop using cat it works fine:  for server in $(cat
/home/kenny/list_of_servers.txt) ; do   ssh $server "uname -a"
Even more baffling to me is that this also works:  while read
server; do   echo $server done < /home/kenny/list_of_servers.txt
Why does my first example stop after the first iteration?
share|improve this question add comment  3 Answers  up vote 10
down vote accepted  The for loop is fine here. But note that
this is because the file contains machine names, which do not
contain any whitespace characters or globbing characters. for x
in $(cat file); do … does not work to iterate over the lines of
file in general, bec