Chris's Wiki :: blog/programming/BourneQuoting Commentshttps://utcc.utoronto.ca/~cks/space/blog/programming/BourneQuoting?atomcommentsDWiki2006-10-16T17:38:05ZRecent comments in Chris's Wiki :: blog/programming/BourneQuoting.By Chris Siebenmann on /blog/programming/BourneQuotingtag:CSpace:blog/programming/BourneQuoting:5504e3f8183f4a7c950317f5542ceacb242b02b8Chris Siebenmann<div class="wikitext"><p>Situations with multilevel quoting are where you really, really want some
form of quoting that nests. The classical example of this in Unix shells is
not actually quoting as such, but command substitutions with backquotes.</p>
<p>Modern Bourne shell versions get around this by supporting a syntax that
nests, <code>$(...)</code>, in addition to the old-style <code>`....`</code>. (<code>rc</code> deals with
the problem in the same way, using <code>`{...}</code> instead.)</p>
</div>2006-10-16T17:38:05ZBy DanielMartin on /blog/programming/BourneQuotingtag:CSpace:blog/programming/BourneQuoting:1604482bba5cb01022ea54c1b5164cf4e901f624DanielMartin<div class="wikitext"><p>What I appreciate so much about the Bourne shell's single-quoting is its absolute predictability. From the open single quote to the very next single quote, every single character - whether backslash, newline, or other generally meaningful character - gets literally copied into the value of the string. After working with trying to remember how exactly to quote or escape in the tenth stupid little language I'm working in today, it's a relief to get to the simplest quoting system on the planet: absolutely nothing quotes the next character. Having to write <code>'\''</code> for a single quote seems like a small price to pay for that complete literal predictability. (though I'll admit that the <code>rc</code> method makes sense)</p>
<p>And it's miles better than what csh, whose quoting system no one can reliably describe to completion. For example, there's the whole brokenness entailed by the fact that this line doesn't generally print a single exclamation point:</p>
<pre>
echo '!'
</pre>
<p>(tcsh, to its credit, makes that line work)</p>
<p>As for the wonderfulness of rc's method, DCL (the command language on VMS) had a similar quoting mechanism. I seem to recall that this could very quickly lead to needing to put eight quotation marks in a row because of some other silliness of DCL that made it hard to re-quote something to pass it to another program.</p>
</div>2006-10-16T04:02:08ZBy Chris Siebenmann on /blog/programming/BourneQuotingtag:CSpace:blog/programming/BourneQuoting:e90f7c57fec4cb1160236525625f020abd6b7a5fChris Siebenmann<div class="wikitext"><p>In hindsight I should have explained this more. The transformation is
done to single quotes inside strings that you are quoting (with single
quotes), so you wind up with things like:</p>
<blockquote><p><code>$ echo 'a string with a '"'"' in it' <br>
a string with a ' in it</code></p>
</blockquote>
<p>The first single quote ends the string, then the <code>"'"</code> bit generates
a quoted single quote, then the final single quote starts up the string
again. (Bourne shell string merging rules then turn it into a single
argument.)</p>
<p>Since <code>\'</code> also generates a quoted single quote, it could be used in
place of the <code>"'"</code> sequence to save a character.</p>
<p>As a standalone sequence <code>'"'"'</code> is indeed a Bourne shell parsing
error (an unterminated double-quoted string).</p>
</div>2006-10-15T02:12:13ZFrom 70.231.136.160 on /blog/programming/BourneQuotingtag:CSpace:blog/programming/BourneQuoting:09da7851ceda5ad62c26058ef5577718aa2c5af6From 70.231.136.160<div class="wikitext"><p>If nothing has special meaning inside single quotes except single quotes, why in the world would '"'"' represent '? It seems like I see: initial single quote (omitted); initial double quote (passed through); second single quote (...?), etc. This would seem to indicate it should either become "'", or more likely, " and then a parsing error.</p>
<p>Is there some extra rule I'm missing, like 'the only special thing inside single-quoted strings are single quotes AND double-quoted strings'? If so, how do you put a " inside a single-quoted string? With \"? Wait, that means there's yet <em>another</em> character that's still special inside single-quoted strings.</p>
<p>-- nothings</p>
</div>2006-10-14T20:45:14Z