[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf %n is not documented

From: Emanuele Torre
Subject: Re: printf %n is not documented
Date: Sat, 24 Dec 2022 02:12:03 +0100

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

> 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.

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?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]