Why quoting in the Bourne shell makes me grumpy
Imagine for a moment that you have an almost arbitrary string (no nulls, newlines, or other funky control characters) that you want to quote so that it passes through the Bourne shell intact. How do you do it?
The Bourne shell offers you two quoting schemes, strings in single quotes and strings in double quotes.
Strings in double quotes need special characters escaped with backslashes. There are five of them (quick, do you know them all?), or six in some situations in some versions of the Bash manpage. And of course, backslash is not a general escaping character; putting it in front of a non-special character is not harmless.
Strings in single quotes can't have anything escaped, which is OK, because nothing has special meaning inside them. Except a single quote. Since there is no way of escaping the single quote, you get to turn a single quote into five characters:
'"'"'
(Okay, I suppose you could turn it into '\''
instead and save yourself
one character, at the expense of generating something that looks even
more like you stuttered.)
(You may or may not be able to read that in your font, since a fair number of fonts are not so great at distinguishing single quotes, double quotes, and apostrophes. It's almost as bad as the great l vs 1 problem.)
This leads to a general view of mine: often, the more quoting methods you have the worse off you are. Unless they are very specialized, quantity serves mostly to confuse, annoy, and surprise; you are better off with one method, ideally one as simple as possible.
Sidebar: so how should shells do quoting?
I'm glad you asked. Tom Duff's rc answered this question many years ago:
A quoted word is a sequence of characters surrounded by single quotes (
'
). A single quote is represented in a quoted word by a pair of quotes (''
).
That's it. As a bonus it's dirt simple to write code that quotes a string for rc: double any single quotes, then put the whole thing in single quotes.
|
|