On Fri, Dec 23, 2022 at 06:53:08PM -0500, Chet Ramey wrote: > On 12/23/22 7:24 AM, Emanuele Torre wrote: > > About two months ago, I discovered, reading the bash source code for > > printf, the %n format specifier. > > > > What it does is assign to the variable specified as its argument, the > > number of bytes that have been written so far (similarly to the C > > counterpart). > > It's a standard part of printf(3), and is standardized by POSIX. Bash > doesn't document the standard conversion specifiers.
It is a standard part of printf(3), and POSIX does standardise it for printf(3) as it is part of the C99 standard. That is the printf() C library function though, not the printf utility (printf(1)). The bash documentation for printf bases itself on printf(1), not printf(3): > In addition to the standard format specifications described in printf(1), > printf interprets: > > %b expand backslash escape sequences in the corresponding argument > %q quote the argument in a way that can be reused as shell input > %(fmt)T output the date-time string resulting from using FMT as a format > string for strftime(3) And the POSIX speficiation for printf(1), that is not based on printf(3) and is independent from it and differs from it in quite a few ways, does not mention %n. <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html> Furthermore, both dash's printf, and the GNU coreutils' printf do not support %n: bash-5.1$ dash -c 'printf "$@"' dash %n a dash: 1: printf: %n: invalid directive bash-5.1$ /bin/printf %n a /bin/printf: %n: invalid conversion specification I am pretty sure %n in printf(1) is an extension. Is it not? emanuele6