[Techtalk] Bash builtin "read" isn't working like I expect

Dominik Schramm dominik.schramm at gmxpro.net
Thu Oct 30 18:07:31 EST 2003


Hi,

> 
> while read LINE; do echo $LINE; grep $LINE $other-file; done < $first-file
> 

The syntax of grep is 
grep expression filepath [filepath ...]

MAYBE... If $LINE contains "special" characters, this may
be misinterpreted by the shell which parses the expressesions between
do and done before executing them. 
E.g. if $LINE=="hey you" and $other-file="myfile" (without "")
the resulting command is 
   grep hey you myfile

"you" is interpreted as another file, and if it doesn't exist, grep exits.

I'd use 
   grep "$LINE" $other-file

instead. This however only works if there is no " in $LINE.

> 
> I don't know "read" well enough to know why it's acting funky. Googleing
> turned up bugs in bash itself and some confusing mentions of the IFS
> variable. I would really appreciate any help. Or general ideas.  
> 

See the bash(1) manual page; far to the bottom there are explanations
for the bash-builtins. There may also be a manual page builtins(1), which
deals exclusively with them.

IFS is the "input field separator", i.e. the character which is used to
split
the input line into fields. This is irrelevant this case, since you assign
the 
whole line to *one* variable (because you are giving read only one variable
name as argument, LINE).

hope this helps
regards,
dominik






More information about the Techtalk mailing list