On Mon, Nov 16, 2026 at 13:34:09 +0100, Vincent Lefevre wrote:
> On 2026-11-16 12:44:55 +0100, Nicolas George wrote:
> > The escaping if fragile, it relies on the fact that a is not a special
> > character for escaping and therefore the backslash in front of it will
> > not have its special semantic.
> 
> Yes, but AFAIK, this is valid. At least code like
> 
>   printf "...\n"
> 
> is very common in shell scripts (including /bin/sh ones).

It's technically wrong, but it works in every shell I've seen, so
people keep using it.

Single quotes are preferred there, both for the \ preservation, and
to discourage people from using expansions in the printf format
argument.  printf "$myvar\\n" is far worse, unless myvar is tightly
controlled and designed to be used as a printf format argument.

printf '%s\n' "$myvar" is one correct way to write it.
printf %s\\n "$myvar" is another.
printf "%s\\n" "$myvar" is another, but I really don't recommend this one.

If you're confused about whether you should be using one \ or two \\,
that's exactly why single quotes are recommended.  Then you always know
you want one \, which will be passed along literally.

Reply via email to