tags:

views:

195

answers:

2

My input file content is

welcome

welcome1

welcome2

My script is

for groupline in `cat file`
do
        echo $groupline;
done

I got the following output.

welcome
welcome1
welcome2 

Why it is not print the empty line. I want the reason.

+1  A: 

Because you're doing it all wrong. You want while not for, and you want read, not cat:

while read groupline
do
  echo "$groupline"
done < file
Ignacio Vazquez-Abrams
yea , this things I know . But I want know why the cat command not giving the actual things to for loop .
muruga
`for` doesn't care about "lines". It only cares about "words", and anything in `$IFS` is used to delimit words. By default it contains `" \t\n"`, which means that newlines are ignored.
Ignacio Vazquez-Abrams
I accept your comment. Not for code. Because I need the code with for only.
muruga
Why do you need the code with for only ?
David V.
+1  A: 

you need to set IFS to newline \n

IFS=$"\n"
for groupline in $(cat file)
do
        echo "$groupline";
done

Or put double quotes. See here for explanation

for groupline in "$(cat file)"
do
        echo "$groupline";
done

without meddling with IFS, the "proper" way is to use while read loop

while read -r line
do
 echo "$line"
done <"file"
ghostdog74