On Sat, Mar 15, 2025 at 08:55:18AM +0100, Christian Schulte wrote: > On 3/15/25 07:37, Otto Moerbeek wrote: > > On Sat, Mar 15, 2025 at 07:29:39AM +0100, Christian Schulte wrote: > > > >> Hi @misc, > >> > >> I recently stumbled upon an issue with GNU printf(1). I was using > >> echo(1) in a testsuite.at on OpenBSD successfully, but that failed on > >> linux badly. The OpenBSD man page of echo(1) contains this sentence: > >> > >> Where portability is paramount, use printf(1). > >> > >> So I replaced echo(1) with printf(1). This leads to... > >> > >> OpenBSD: > >> > >> x500$ printf -0 > >> -0x500$ printf -something > >> -somethingx500$ > >> > >> which is the expected output. On linux I get > >> > >> schulte@vps:~$ printf -0 > >> -bash: printf: -0: invalid option > >> printf: usage: printf [-v var] format [arguments] > >> schulte@vps:~$ printf -something > >> -bash: printf: -s: invalid option > >> printf: usage: printf [-v var] format [arguments] > >> > >> Would you rate this a bug in GNU printf(1)? > >> > >> -- > >> Christian > >> > > > > No, i would rate as a usage error. Use -- to separate options form the > > rest of the arguments. > > > > print -- -0 > > > > -Otto > > Thanks. Adding -- everywhere should make it portable. Would you mind > explaining the following to me as well? > > For example: > > x500$ echo -n \\u0041 > \u0041x500$ > > Expected output. > > x500$ printf -- \\u0041 > printf: unknown escape sequence `\u' > u0041x500$ > > x500$ printf -- \\\u0041 > printf: unknown escape sequence `\u' > u0041x500$ > > x500$ printf -- \\\\u0041 > \u0041x500$ > > Why does printf(1) require four backslashes to yield the same output? > The man page just mentions: > > \\ Write a backslash character. > > I would have expected \\\\ to write two backslash characters. Is this > related to the shell, or to the command? > > -- > Christian >
It's the shell plus echo does not care about backslashes while printf does, and \u is an unknown escape sequence to printf. See the shell man page of how shell quoting and backslashes work (Command syntax and Quoting sections). -Otto