for tline in $(cat nh.txt)
do
echo "**********"
echo $tline
done
I get this output:
**********
user1
**********
email1
**********
email2
**********
user2
**********
email1
**********
email2
etc.
ie. the 'cat' command seems to be supplying one field at a time, rather than the whole line - is that right?
That is a dangerous use of backticks and useless use of cat and discovered for yourself one of the reasons why You are correct, it splits on whitespace, not lines. (You can change this by altering the special IFS variable, but read is preferred over backticks for this anyway.)
If you want to read lines, you can do this.
while read LINE
do
echo "got line $LINE"
done < inputfile
This is also more efficient than cat in backticks.
It can even split tokens for you:
while read USERNAME EMAIL1 EMAIL2
do
echo "user $USERNAME"
echo "Email1 $EMAIL1"
echo "Email2 $EMAIL2"
done < inputfile
read also obeys IFS, so if your file was separated by commas instead of spaces, the loop would still work with a tiny change:
while IFS="," read USERNAME EMAIL1 EMAIL2
do
echo "user $USERNAME"
echo "Email1 $EMAIL1"
echo "Email2 $EMAIL2"
done < inputfile