[ale] bash question

Michelangelo Grigni mic at mathcs.emory.edu
Sat Jun 12 21:46:22 EDT 1999


> == joe
> > == bob
> >If I give the bash command cat t1.txt, I get the following
> >
> >This is line1
> >This is line2
> >This is line3
> >
> >However, if I do:
> >
> >for f in `cat t1.txt` ; do echo $f ; done
> >
> >I get the following
> >
> >This 
> >is 
> >line1
> ...
> I can think of a bunch of ways, but they're all really convoluted

This is a job for the shell IFS variable.  Just set it to
contain a single newline, and then each line will be
treated as a single input field.   Like this:

IFS='
'
for f in `cat t1.txt` ; do echo $f ; done

Or like this, without cat:

IFS='
'
while read f; do echo $f; done < t1.txt


You may need to restore IFS to its default, depending on
what else you do in the script.






More information about the Ale mailing list