Why is a single backslash shown when using quotes

Section 3.1.2.3 Double Quotes of the GNU Bash manual says:

The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed.

Thus \ in double quotes is treated differently both from \ in single quotes and \ outside quotes. It is treated literally except when it is in a position to cause a character to be treated literally that could otherwise have special meaning in double quotes.

Note that sequences like \', \?, and \* are treated literally and the backslash is not removed, because ', ? and * already have no special meaning when enclosed in double quotes.


Backslash is interpreted differently according context:

  • Within double quotes (your first example):

    The backslash  retains its special meaning  only when followed
    by one of the following characters: $, `, ", \, or <newline>.
    
  • Without quotes (your second example):

    A  non-quoted  backslash  (\)  is the  escape  character.   It
    preserves  the  literal  value  of  the  next  character  that
    follows,  with the  exception of  <newline>.  If  a \<newline>
    pair  appears, and  the backslash  is not  itself quoted,  the
    \<newline> is treated  as a line continuation (that  is, it is
    removed from the input stream and effectively ignored).
    
  • Using the construct $'....', where you can use inside the quote the standard backspace character, nearly as in C. e.g. \n, \t, etc.

  • Using backquotes:

    When  the old-style  backquote form  of substitution  is used,
    backslash retains its literal  meaning except when followed by
    $, `, or \.
    

Source of quotes: bash manual