bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: quote removal issues within character class


From: Robert Elz
Subject: Re: quote removal issues within character class
Date: Sun, 10 Nov 2019 00:11:57 +0700

    Date:        Sat, 09 Nov 2019 06:46:05 -0800
    From:        L A Walsh <address@hidden>
    Message-ID:  <address@hidden>

  | Is this really what the standard says,

Yes, I used cut&paste (and then some line length/wrappoing reformatting)

  | because '\\' is not a character, but 2 characters.

In sh, yes, in C, no.   So it all depends upon context.  The text comes
from the XBD (basic definitions) so isn't either sh or C specific.

But since that is followed by (and you quoted)
        (<period>, <asterisk>, <left-square-bracket>, and <backslash>,
        respectively)
I don't thnk there is any doubt what it means.

  | They could use "\\" but if a backslash is between single
  | quotes, it loses its special meaning.

In sh yes, but this text is from the section on Regular Expressions,
in the basic definitions (XBD), not the (many) sections about the shell
in XCU (Commands and Utilities).

  | The only way to get a backslash

get in what sense?   In sh '\' is just fine, as is \\ if what you
want is a literal backslash.

  | when using single quotes that I've found is to end the single-quote
  | then use the backslash.

If you want to \ escape a character when you're in single quotes, then
yes (in sh) that is correct.   But the only time that makes sense is if
the character that is to follow the \ is a ' (which cannot appear in a
single quoted string), or if you want to elide a newline \<nl> (where
the <nl> means a literal newline character).

  | So if you wanted to insert single quotes in
  | a string that is single-quoted, you would have to do this:
  |
  | 'this is a single-quote(SQ) quoted string using a SQ ('\'') within
  | the single quote.' 

That is the usual way, yes.

  | Alternatively using double-quotes or another quoting
  | mechanism might be preferable.

Yes, there are several other ways to do it, but the \' form is
certainly the most common.   When $'...' quoting becomes more popular
(after it actually makes it into the standard) then that form allows
        $' ...\' ... '
($' is "C" quoting, it works just like single quotes, except that C style
backslash escape sequences, plus a few new ones, are expanded).

  | I find it odd that the standard would try to use a backslash within
  | a SQ'd string as a literalizer.

It all relates to the context, and the expectations of the reader.  Since
the parenthisised note that follows is explicit about what it means, I
don't think it really matters.   In the section about the shell, \ tends
to be written exactly like that (no quo9ting, just the character) when it
is needed (it might occasionally be written as <backslash>).

kre




reply via email to

[Prev in Thread] Current Thread [Next in Thread]