[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Where is the usage of '.' in printf documented?
From: |
Eric Blake |
Subject: |
Re: [Help-bash] Where is the usage of '.' in printf documented? |
Date: |
Thu, 19 Mar 2015 15:36:11 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 |
On 03/19/2015 02:56 PM, Peng Yu wrote:
> Hi,
>
> I see the following usage of '.'. But I can't find it in `man bash`
> nor `man 3 printf`. Does anybody know where it is documented? Thanks.
Hmm - you found a bug in the manual. 'man bash' says:
In addition to the standard printf(1) format specifications,
but that link should be pointing to printf(3), not printf(1).
>
> printf "%0.s-" {1..10}
>
At any rate, reading 'man 1 printf' gets you coreutils' page (at least
on my system), which says:
and all C format specifications ending with one of diouxXfeEgGcs,
with
ARGUMENTs converted to proper type first. Variable widths are
handled.
which is an implicit pointer to printf(3) (the C format specification
rules), and reading 'man 3 printf' shows:
The precision
An optional precision, in the form of a period ('.') followed
by an
optional decimal digit string. ... If the precision is given as
just '.', the precision is taken to be zero. ... This gives ...
the maximum number of characters to be printed from a
string for s and S conversions.
As it is, your format string is undefined, because you are using the '0'
flag with the 's' specifier:
0 The value should be zero padded. For d, i, o, u, x, X, a,
A, e,
E, f, F, g, and G conversions, the converted value is
padded on
the left with zeros rather than blanks. If the 0 and -
flags
both appear, the 0 flag is ignored. If a precision is
given
with a numeric conversion (d, i, o, u, x, and X), the 0
flag is
ignored. For other conversions, the behavior is undefined.
but assuming that you had written "%.s-" instead of "%0.s-" as your
format string, it would mean that you were specifying 0 precision (print
0 bytes of each string - effectively ignoring whatever string argument
you had given) followed by the -. And even though the '0' flag is not
required to work with 's', many printf implementations silently ignore
it as a no-op.
Here's another test you can do to see the '.' precision making a
difference, but here without a precision of 0:
$ printf "%.1s-" {1..10}
1-2-3-4-5-6-7-8-9-1-
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature