On Mon, Nov 16, 2026 at 03:53:51PM +0100, Nicolas George wrote:
> Vincent Lefevre (HE12026-11-16):
> > I would have said that backslash + ASCII letter ([A-Za-z]) inside
> > double quotes is safe (probably with digits too) as used in many
> > scripts, and such characters are obviously not characters that need
> > escaping.
> 
> Why would it be OBVIOUS that the shell do not use the same escaping
> sequences as printf or the C language?
> 
> Also: are you sure of your statement, to the point of risking your
> health on it? If not, be safe.

If everything fails, read the documentation. It's not clear which shell
is meant here, but I stick to bash. The manual page (section "QUOTING")
basically says:

    There  are  three  quoting  mechanisms:  the  escape  character, single
  quotes, and double quotes.

  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  it‐
  self quoted, the \<newline> is treated as a line continuation (that is,
  it is removed from the input stream and effectively ignored).

  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.

  Enclosing characters in double quotes preserves the  literal  value  of
  all  characters  within the quotes, with the exception of $, `, \, and,
  when history expansion is enabled, !.  When the shell is in posix mode,
  the  !  has  no special meaning within double quotes, even when history
  expansion is enabled.  The characters $  and  `  retain  their  special
  meaning  within double quotes.  The backslash retains its special mean‐
  ing only when followed by one of the following characters: $, `, ",  \,
  or  <newline>.   A  double  quote may be quoted within double quotes by
  preceding it with a backslash

I'll leave the rest to you to read. One thing is that, whithin double quotes,
the escape just turn $, `, <newline> and \ into "normal" characters. So yes,
"\a" is backslash-then-a (I don't know whether this is what the OP means:
note than in C and many other languages this is different!

If you want to express special characters with backslash sequences (as you
might be accustomed from C), you have to spell it out like so:

  echo -n $'\a' # a bell character

Alternatively, you can use echo's -e option and let echo make the translation,
instead of your shell's reader.

Cheers
-- 
t
> 
> > And Debian (for instance) keeps distributing such scripts.
> 
> Debian does what it does, that has no relevance on advice given to
> people who want to program robustly.
> 
> > There are cases when one wants to be able to do expansion, knowing
> > that this is safe. For instance:
> 
> Yes, but what is your point?
> 
> -- 
>   Nicolas George
> 

Attachment: signature.asc
Description: PGP signature

Reply via email to