[PLUG] 'date' output cli/bash inconsistency
Felix Lee
felix.1 at canids.net
Sat Aug 2 20:07:01 UTC 2003
"Steven Raymond" <stever at woo-hoo.com>:
> > Echo strips out extra spaces, enclose the variable in quotes
> > echo "$TODAY"
> You're right! Thank you. So I checked "man echo" and "info echo" and
> neither mentioned this fact. Is there some oft-found reason that would
> make it obvious that the quotes are needed?
It's not echo doing that, it's the shell. When you do a
$-substitution, the substitution gets word-split, unless it's
enclosed in quotes.
So the shell splits $TODAY into words, losing all the spaces. It
passes the words to echo as separate args, then echo pastes them
back together again, putting a single space between the words.
Since most of the time I don't want word-splitting, I make it a
habit to use quotes around $-substitutions even when it isn't
strictly necessary. That makes it clear (to me at least) when
word-splitting is the desired effect, such as
files="a b c"
out="x"
cat $files > "$out"
--
More information about the PLUG
mailing list