2009年4月12日星期日

while i=`line`与while read i的区别


a)
while i=`line`
do
echo "i is set to $i"
done < my_file

b)
while read i
do
echo "i is set to $i"
done < my_file

there are a few differences. e.g., try inputting a line
ending with backslash ("\"), such as:

first line \
second line

the read i version reads both lines into one instance of
$i (sans the escaping backslash: "first line second line").
but the `line` version executes twice, first reading the
backslash-ending line (with the backslash) into an instance
of $i ("first line \"), and the second time 'round the loop
the subsequent line into $i ("second line").

another difference: leading spaces/tabs are stripped in
the read i version (at least in some versions of various
shells; i cannot recall if all shells have always done this
or not).

refers to Heiner Steven's unix shell threads

没有评论: