Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does \$ reduce to $ inside backquotes [though not inside $(...)]?
    text
    copied!<p>Going over the POSIX standard, I came across another rather technical/pointless question. It <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03" rel="nofollow">states</a>:</p> <blockquote> <p>Within the backquoted style of command substitution, <code>&lt;backslash&gt;</code> shall retain its literal meaning, except when followed by: '<code>$</code>' , '<code>`</code>' , or <code>&lt;backslash&gt;</code>. </p> </blockquote> <p>It's easy to see why '<code>`</code>' and '<code>\</code>' lose their literal meanings: nested command substitution demands a "different" backquote inside the command substitution, which in turn forces '\' to lose its literal meaning. So, for instance, the following different behavior seems reasonable:</p> <pre><code>$ echo $(echo \\\\) \\ $ echo `echo \\\\` \ </code></pre> <p>But what about '$'? I.e., what's the point or, more concretely, a possible benefit of the following difference?</p> <pre><code>$ echo $(echo \$\$) $$ $ echo `echo \$\$` 4735 </code></pre> <p>As '$' by itself is not ruled out inside backquotes, it looks like you would use either '$' or '\\\$' all the time, but never the middle case '\$'. </p> <p>To recap,</p> <pre><code>$ echo `echo $$` # PID, OK 4735 $ echo `echo \\\$\\\$` # literal "$$", OK $$ $ echo `echo \$\$` # What's the point? 4735 </code></pre> <p>PS: I know this question is rather technical... I myself go for the more modern <code>$(...)</code> substitution all the time, but I'm still curious.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload