[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why no closing single quote is needed when a single quote is escaped
From: |
Eli Schwartz |
Subject: |
Re: Why no closing single quote is needed when a single quote is escaped? |
Date: |
Thu, 31 Dec 2020 19:13:20 -0500 |
On 12/31/20 6:35 PM, Peng Yu wrote:
x='\'
The difference between single quoting and double quoting is fully
explained in the bash manual page, under the section "QUOTING".
The man page says the following. But it doesn't say why a closing
single quote is not need to pair with the first quote. Why \' makes
the closing single quote unnecessary.
Enclosing characters in single quotes preserves the literal
value of each character within the quotes. A
single quote may not occur between single quotes, even when
preceded by a backslash.
If this sentence is not the relevant description in the manpage,
please be specific which sentence is, as I don't find it. Thanks.
Those two sentences are indeed the relevant description. Due to the
first sentence, there is no such thing as the syntax token "backslash"
(i.e. "the escape token") inside single quotes (because it is merely an
ASCII string character).
The second sentence makes the witty observation that since there is no
such thing as the syntax token backslash inside of single quotes, you
cannot use nonexistent things to escape a single quote, hence a single
quote will always indicate "end of quoted string". You cannot tell bash
that it is actually an ASCII string character.
Remember your original example:
x='\'
You asked:
It doesn't make sense to me why the above command should work > without closing
the initial single quote.
But your question itself makes no sense.
$ echo "«$x»"
«\»
Note how the variable does not contain a single quote, so it could not
have been escaped and turned into a string.
x='\'
x=''
^contents go here
contents are one byte, being the unicode codepoint U+005C
a.k.a. the backslash, a.k.a. \
Meanwhile, the double quotes did indeed first refuse to end (since \"
got parsed as a string character, so <ENTER> prompted a multiline
command with the line continuation PS2 "> ")
$ x="\"
> "
$ echo "«$x»"
«"
»
Note the double quote there, indicating it did not get used as a syntax
end-of-string marker. Instead, the string contained the newline too, and
ended on the second double quote.
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
OpenPGP_signature
Description: OpenPGP digital signature