Difference between whitespaces " " and '\ ' in bash
Is there any difference between " "
and \
in b开发者_运维技巧ash?
For example, ls *" "*
and ls *\ *
will return the same result for me.
the ""
version will incur a teensy bit more overhead to parse the innards of the quotes for any potential variables/operators. The backslash-space version will be seen as only a literal space without the extra overhead. The actual cost in cpu time/memory usage between the two will be utterly microscopic, however, unless it's inside a loop that's being run kajillions of times.
The backslash only masks the next character, and might be faster typed, if you just want to mask one, but that might depend on your keyboard layout, and whether you type with 10 fingers blindly or not. I prefer quotes, because they are more easy to type.
Masking each other and oneself is another difference:
echo \\
will output a backslash, but
echo "\"
will output a ", and wait for the closing quote.
echo \"
will work, and output a quote, but
echo """
will not, for the same reason as above.
Some special characters like linefeed, formfeed, tab, alert and so on can be produced with backslash only, but not with quotes, including backslash itself.
/bin/echo -e "\tfoo\abar\bbar\ffoo\nbar\rfoo"
See man echo
for a complete list.
That's not only true for echo
, but for many things you can do in scripts, working with strings.
精彩评论