4
echo "     a" | while read; do echo "$REPLY"; done

will output ".....a" which contains leading white space. However,

echo "     a" | while read line; do echo "$line"; done

will output "a" with leading white space skipped (OK, because word splitting).

It seems the REPLY variable has the same effect of IFS set to null:

echo "     a" | while IFS= read line; do echo "$line"; done

From the bash manual, I can't find the reason. Do you have any ideas? Thanks.

1 Answer 1

2

From read man page:

Read one line from the standard input, (or from a file) and assign the word(s) to variable name(s).

If no names are supplied, the line read is assigned to the variable REPLY.

So, $REPLY is always the whole line, while the assigned variables are always words. It would not work otherwise e.g. if you have multiple words, what would $REPLY be?

1
  • OK, so a line is read, the word split is not perform yet.
    – user315311
    Oct 11, 2018 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .