[prog] assigning variables in sh
Peter Samuelson
peter at p12n.org
Wed Jan 12 14:00:37 EST 2005
[Almut Behrens]
> and there should be whitepace around the test brackets, i.e. "while [
> $LINE ]; do", not "[$LINE]".
That's a bad habit to get into, and it might be non-portable as well.
What you should do is quote the $LINE variable:
while [ "$LINE" ]; do
It's a bad habit because, in many situations, you can't be sure that
your input will not have special characters like spaces. if $LINE has
a space in it, you'll get a syntax error or worse, depending on the
shell. Perhaps *this* time you can be sure that $LINE won't have
spaces in it, but I'm talking in general.
The reason the original might also be non-portable is that if $LINE
happens to be empty, the expression will expand to "while [ ]; do"
which might throw a syntax error with some shells. I note that the
bash builtin test, and the test in GNU coreutils, do not spew such a
message - but I could have sworn that some version of Unix does.
Peter
More information about the Programming
mailing list