Date:        Mon, 1 Jun 2026 16:09:33 +1200
    From:        Martin D Kealey <[email protected]>
    Message-ID:  
<can_u6mw83zstnbqpu3-amvmvax2tjcbyehr9kcjxflo_wkv...@mail.gmail.com>

I'm not sure what any of this has to do with the original issue, but:

  | 1. Backslash is not an "escape", but rather a way of quoting

That's true, of backslashes that appear unquoted on the command line, it
isn't true of backslashes that appear from expansions, or which are themselves
quoted, which can act as an escape in all kinds of ways (including in
patterns, which is relevant to the original question, or \ inside $''
quoted strings).

  | 2. Quote stripping is only done ONCE, BEFORE variable expansions.

The first half of that is right, the second is wrong.  Quote stripping
is done last of all.   What is is "different" from what might be expected
is that only those quote characters which acted as quoting chars on the
command line are stripped (ie: the actual quoting chars) - characters which
simply look like quote characters, but aren't being used to quote anything,
are left alone (this includes "quote" characters that were themselves quoted,
and those that result from expansions).

If this weren't true, then (as just one example) "$@" would be the same
as $@ as the quotes would already have been removed.

  | 3. Exception: inside double quotes, $ and ` remain unquoted

That's a bizarre way of thinking of it, it isn't that they're unquoted
(which they would be, as the quotes were apparently already removed in
step 2!) but that within "" expansions still happen - the results are
then quoted.   That's the primary difference between ' (and $') quoting,
and " quoting (it certainly isn't anything like C, where the two produce
entirely different data types).

  | During this phase, backslashes are removed (and the next char
  | always marked as quoted)

All chars inside "" are quoted.   Including the results of expansions
that occur inside the double quotes.

  | when each precedes $ ` or another backslash.

or a " which would otherwise end the quoting (which you used in the
illustrative examples).

  | 4. After variable expansion, parts that were NOT quoted are considered for
  | wordsplitting and then globbing.

But only when in a context where those things happen, not always.
And not just variable expansion, all expansions (parameter, not just
variable, the numeric & special parameters too, also arith expansions and
command substitutions.)

  | To illustrate it's worth considering that these are EXACTLY synonymous:
  |
  | $ foo='a\bc "x y z"'
  | $ foo="a\\bc \"x y z\""
  | $ foo=a\\bc\ \"x\ y\ z\"

Yes, but perhaps more importantly, so is

        foo="a\bc \"x y z\""

because \ is only a quote/escape char inside "" when the char which
follows it is one of the small group that might need it, so a literal \
that is not followed by a \ " $ or ` can be written as one, or two, \
chars in a double-quoted string.   (Always two when unquoted, or in $'
quoting, always one in single quotes, only in double quotes is there
the option which way to write it, and only when one of the magic 4
chars don't come next - though to be absurdly complete, in $' quotes
it is also possible to use \x5c or \134 or even \u005c to represent
a literal \ instead of the more likely \\).

However, why anyone would write anything but the first would perplex me.

In general, use single quoting (or perhaps \ quoting if there are just one
or two chars in the word that need it) in shell, except when the rich set
of escape sequences that $' quoting provides is needed, or when the string
needs an expansion inside it (in that case use "" quoting).

kre



Reply via email to