Thread View: linux.debian.user
15 messages
15 total messages
Started by Vincent Lefevre
Mon, 16 Nov 2026 11:40
Re: beeps and other sounds
Author: Vincent Lefevre
Date: Mon, 16 Nov 2026 11:40
Date: Mon, 16 Nov 2026 11:40
25 lines
2708 bytes
2708 bytes
On 2024-10-18 22:08:09 -0400, Timothy M Butterworth wrote: > I googled around and found: > > - tput bel: This command uses the tput utility to send the "bell" > control character to the terminal. > - echo -e "\a": The -e option enables interpretation of backslash > escapes, and \a represents the bell character. The -e option is not portable. For instance, with dash (/bin/sh by default): $ echo -e "\a" -e and \a is interpreted as bell anyway, but as said in the dash(1) man page, this is undefined behavior; in short, don't do that! > - printf "\a": The printf command works similarly to echo -e "\a". AFAIK, this one is portable. -- Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)
Re: beeps and other sounds
Author: Nicolas George
Date: Mon, 16 Nov 2026 11:50
Date: Mon, 16 Nov 2026 11:50
24 lines
3065 bytes
3065 bytes
Vincent Lefevre (HE12026-11-16): > > - printf "\a": The printf command works similarly to echo -e "\a". > AFAIK, this one is portable. 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. For a more robust escaping, prefer: printf "\\a" → the backslash is properly escaped. printf '\a' → no escaping needed in single quotes. printf $'\a' → less portable, the shell does the \a→BEL translation, not the printf command. printf → type it with ctrl-V for example, no escaping needed. Regards, -- Nicolas George
Re: beeps and other sounds
Author: Vincent Lefevre
Date: Mon, 16 Nov 2026 12:40
Date: Mon, 16 Nov 2026 12:40
15 lines
2484 bytes
2484 bytes
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). -- Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)
Re: beeps and other sounds
Author: Nicolas George
Date: Mon, 16 Nov 2026 12:50
Date: Mon, 16 Nov 2026 12:50
18 lines
3010 bytes
3010 bytes
Vincent Lefevre (HE12026-11-16): > Yes, but AFAIK, this is valid. At least code like > printf "...\n" > is very common in shell scripts (including /bin/sh ones). Using echo -n is quite common too. I thought you were arguing for something as standard and robust as possible. Nothing that relies on a comprehensive list of special characters is robust. Can you tell, without looking at the standard, the exact list of characters in front of which backslash is an escaping character rather than the character backslash itself? And can you tel, for most common implementations of the shell, if they conform to the standard? Regards, -- Nicolas George
Re: beeps and other sounds
Author: Greg Wooledge
Date: Mon, 16 Nov 2026 13:00
Date: Mon, 16 Nov 2026 13:00
27 lines
3098 bytes
3098 bytes
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.
escaping with \ in shells (was: beeps and other sounds)
Author: Vincent Lefevre
Date: Mon, 16 Nov 2026 14:10
Date: Mon, 16 Nov 2026 14:10
68 lines
4555 bytes
4555 bytes
On 2026-11-16 13:42:34 +0100, Nicolas George wrote: > Vincent Lefevre (HE12026-11-16): > > Yes, but AFAIK, this is valid. At least code like > > printf "...\n" > > is very common in shell scripts (including /bin/sh ones). > > Using echo -n is quite common too. I thought you were arguing for > something as standard and robust as possible. Nothing that relies on a > comprehensive list of special characters is robust. > > Can you tell, without looking at the standard, the exact list of > characters in front of which backslash is an escaping character rather > than the character backslash itself? And can you tel, for most common > implementations of the shell, if they conform to the standard? 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. This should really be standardized if not done yet. On 2026-11-16 07:54:18 -0500, Greg Wooledge wrote: > 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. And Debian (for instance) keeps distributing such scripts. > 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. If the string contains the ' character, one would typically use double quotes. > 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. There are cases when one wants to be able to do expansion, knowing that this is safe. For instance: printf "Exit status: $?\n" BTW, /usr/bin/annotate-output in devscripts has similar code: printf "$?\n" >&2; err="$?"; printf "Finished with exitcode $err\n" | handler I; exit "$err"; -- Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Nicolas George
Date: Mon, 16 Nov 2026 15:00
Date: Mon, 16 Nov 2026 15:00
24 lines
2993 bytes
2993 bytes
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. > 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
Re: escaping with \ in shells (was: beeps and other sounds)
Author: tomas
Date: Mon, 16 Nov 2026 15:40
Date: Mon, 16 Nov 2026 15:40
92 lines
5325 bytes
5325 bytes
--ev0zGggavvS2Q9hA
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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.
>=20
> Why would it be OBVIOUS that the shell do not use the same escaping
> sequences as printf or the C language?
>=20
> 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=E2=
=80=90
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=E2=
=80=90
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 quote=
s,
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 translati=
on,
instead of your shell's reader.
Cheers
--=20
t
>=20
> > And Debian (for instance) keeps distributing such scripts.
>=20
> Debian does what it does, that has no relevance on advice given to
> people who want to program robustly.
>=20
> > There are cases when one wants to be able to do expansion, knowing
> > that this is safe. For instance:
>=20
> Yes, but what is your point?
>=20
> --=20
> Nicolas George
>=20
--ev0zGggavvS2Q9hA
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCavsh6wAKCRAFyCz1etHa
RkNoAJ9Lomr22u8C6rKW/rNhow3fixFZKACeNNG8iLuNYUx9bszjpFMRANv7mCc=
=D8ye
-----END PGP SIGNATURE-----
--ev0zGggavvS2Q9hA--
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Nicolas George
Date: Mon, 16 Nov 2026 15:50
Date: Mon, 16 Nov 2026 15:50
10 lines
2629 bytes
2629 bytes
tomas@tuxteam.de (HE12026-11-16): > If everything fails, read the documentation. It's not clear which shell > is meant here You could stop there: not knowing exactly what shell is meant implies one must use the syntax that will be supported by all of them, as much as possible. -- Nicolas George
Re: escaping with \ in shells (was: beeps and other sounds)
Author: tomas
Date: Mon, 16 Nov 2026 16:00
Date: Mon, 16 Nov 2026 16:00
38 lines
3095 bytes
3095 bytes
--AM2WgOlfQqzPwtWm Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Nov 16, 2026 at 04:47:08PM +0100, Nicolas George wrote: > tomas@tuxteam.de (HE12026-11-16): > > If everything fails, read the documentation. It's not clear which shell > > is meant here >=20 > You could stop there: not knowing exactly what shell is meant implies > one must use the syntax that will be supported by all of them, as much > as possible. =2E..or just the start of an enjoyable collective answer. What's your preferred shell? How does it handle quoting? C'mon, that's what a mailing list is for :-) (FWIW, I'd bet that most "modern" shells aren't that far away from bash. Perhaps excepting the $'\<foo>' notation. Cheers --=20 t --AM2WgOlfQqzPwtWm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCavsm1gAKCRAFyCz1etHa RlXyAJ9zdnlhcr22NN9bRipCoTQGr6YCRgCfSNGIFfgZStI7veLm/DZK4NfL5Pc= =tHSf -----END PGP SIGNATURE----- --AM2WgOlfQqzPwtWm--
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Greg Wooledge
Date: Mon, 16 Nov 2026 16:20
Date: Mon, 16 Nov 2026 16:20
13 lines
2534 bytes
2534 bytes
On Mon, Nov 16, 2026 at 16:53:02 +0100, tomas@tuxteam.de wrote: > (FWIW, I'd bet that most "modern" shells aren't that far away from > bash. Perhaps excepting the $'\<foo>' notation. That quoting form is actually specified in the current POSIX standard: <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_02_04> Many /bin/sh implementations won't have caught up yet, though. That standard is only 2 years old. If you're looking for bash features that POSIX doesn't have, there are plenty more, such as arrays, associative arrays, process substitutions, and so on.
Re: escaping with \ in shells (was: beeps and other sounds)
Author: tomas
Date: Mon, 16 Nov 2026 16:40
Date: Mon, 16 Nov 2026 16:40
43 lines
3444 bytes
3444 bytes
--2O4XV2m64gq34JEt Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Nov 16, 2026 at 11:18:26AM -0500, Greg Wooledge wrote: > On Mon, Nov 16, 2026 at 16:53:02 +0100, tomas@tuxteam.de wrote: > > (FWIW, I'd bet that most "modern" shells aren't that far away from > > bash. Perhaps excepting the $'\<foo>' notation. >=20 > That quoting form is actually specified in the current POSIX standard: > <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.htm= l#tag_19_02_04> >=20 > Many /bin/sh implementations won't have caught up yet, though. That > standard is only 2 years old. Aha. I checked and can confirm that dash hasn't caught up yet. > If you're looking for bash features that POSIX doesn't have, there are > plenty more, such as arrays, associative arrays, process substitutions, > and so on. No, I was rather looking at quoting/escaping, trying to not veer off too far from $SUBJECT ;-) Cheers --=20 t >=20 --2O4XV2m64gq34JEt Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCavsw5wAKCRAFyCz1etHa RoeDAJ9ai2Hny4AeINZNdF/IrD4BwSXpVgCdFHT8clgdYiT0nW3SSKCe2QpW1wQ= =etN3 -----END PGP SIGNATURE----- --2O4XV2m64gq34JEt--
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Vincent Lefevre
Date: Tue, 17 Nov 2026 02:40
Date: Tue, 17 Nov 2026 02:40
34 lines
3006 bytes
3006 bytes
On 2026-11-16 15:53:51 +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? Contrary to the escape sequences in C, the purpose of backslash in a shell is to "preserve the literal value of the following character". Doing otherwise would be a source of confusion. > > 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? Using single quotes is not possible in these cases. $ printf "$?\n" 0 but $ printf '$?\n' $? -- Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Greg Wooledge
Date: Tue, 17 Nov 2026 03:50
Date: Tue, 17 Nov 2026 03:50
31 lines
2669 bytes
2669 bytes
On Tue, Nov 17, 2026 at 03:33:17 +0100, Vincent Lefevre wrote:
> Using single quotes is not possible in these cases.
>
> $ printf "$?\n"
> 0
>
> but
>
> $ printf '$?\n'
> $?
The pedantically correct way to do this would be
printf '%s\n' "$?"
Or %d if you prefer, since the argument will be numeric with no
leading zeroes.
Yes, it's true that "$?\\n" is safe, because we're guaranteed that $?
will never contain any characters that have special meaning in a
printf format argument. But that's playing a dangerous game. In a
future update, someone may see that and decide to add some more
variable expansions to the first argument of printf, and then it
stops being safe.
Besides, if you're going to take shortcuts that are only made possible
by knowledge of content restrictions, just use
echo "$?"
Why only take *one* shortcut when you can take them all?
Re: escaping with \ in shells (was: beeps and other sounds)
Author: Nicolas George
Date: Tue, 17 Nov 2026 07:50
Date: Tue, 17 Nov 2026 07:50
15 lines
2926 bytes
2926 bytes
Vincent Lefevre (HE12026-11-17): > Contrary to the escape sequences in C, the purpose of backslash in a > shell is to "preserve the literal value of the following character". > Doing otherwise would be a source of confusion. The backslash of C also servers the purpose of protecting " from ending a string. The rest is bonus. > Using single quotes is not possible in these cases. Let us just say you need to read the documentation of the printf command. -- Nicolas George
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads