Simple cat and echo question

Apologies, probably a really simple problem:

I've got a text file (nh.txt) with this in it:

    user1 email1 email2
    user2 email1 email2
    etc

With the following basic script:

    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?

What do I do to get the whole line at one go?

Thanks.

That is a dangerous use of backticks and useless use of cat and discovered for yourself one of the reasons why :slight_smile: 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
while read tline
do
 echo "**********"
 echo "$tline"
done < nh.txt

EDIT: Corona688, you are scorchingly fast...!!!

Thanks guys

('dangerous use of backticks' - like the description :D)