Wandering Thoughts archives


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.

programming/BourneQuoting written at 23:27:55; Add Comment

Link: Warning Signs for Tomorrow

Warning Signs for Tomorrow is a collection of warning signs for tomorrow. Amusing and potentially useful ones (for me; your mileage may vary) include 'lack of internet connectivity', 'ubiquitous surveillance', and 'motivation hazard'.

(From James Nicoll.)

links/WarningSignsForTomorrow written at 15:31:45; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.