[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printf %n is not documented
From: |
Greg Wooledge |
Subject: |
Re: printf %n is not documented |
Date: |
Fri, 23 Dec 2022 20:11:15 -0500 |
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.
The GNU printf(3) page says:
n The number of characters written so far is stored into the inte‐
ger pointed to by the corresponding argument.
And POSIX fprintf(3p):
n The argument shall be a pointer to an integer into which is
written the number of bytes written to the output so far by
this call to one of the fprintf() functions. No argument is
converted.
It's not intuitive that the shell's version of a pointer-to-int is
a variable name. It makes sense once it's been stated, but it's not
something that most people are going to guess immediately. I would've
assumed this operation simply wasn't supported in the shell at all.
So, it might be worth stating in the documentation.