[lug] Simple (?) bash+grep issue

David Anselmi anselmi at anselmi.us
Wed May 11 21:09:08 MDT 2005


Jeff Schroeder wrote:
[...]
> var=`grep "stuff" file.txt`
> 
> Then I step through the results to run my shell command:
> 
> for item in $var ; do echo $item ; done
> 
> Obviously this is a much-simplified example.  The problem is that $item, 
> in the for-loop above, contains a trailing newline.

I think you're wrong.  When I do the above I get a number of lines with 
one word on each.  There are no blank lines.  If I add -n to the echo I 
get all the words on one line (no white space) and my prompt on the same 
line (no trailing line feed).

$var doesn't have a line feed at the end.  Try:

var=`grep "stuff" file.txt` ; echo $var | wc -l

and you'll get one line--the line feed comes from echo (and echo -n will 
give you 0 lines).

Perhaps you have an odd version of bash, or you're doing something odd 
in your grep.  Can you give a more illustrative example?

Dave



More information about the LUG mailing list