[Top][All Lists]

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

Re: printf builtin: '*' broken in conversion spec field width and precis

From: Greg Wooledge
Subject: Re: printf builtin: '*' broken in conversion spec field width and precision (e.g., "%.*s")
Date: Thu, 25 Mar 2010 08:23:21 -0400
User-agent: Mutt/

On Thu, Mar 25, 2010 at 03:32:25AM -0700, Rob Robason wrote:
> The printf builtin is broken in many variations of use of '*' (e.g., "%.*s") 
> in a printf conversion spec to set field width and precision from argument 
> values.

The use of '*' as a field width is not required by POSIX.  According to

"No provision is made in this volume of POSIX.1-2008 which allows
field widths and precisions to be specified as '*' since the '*'
can be replaced directly in the format operand using shell variable
substitution. Implementations can also provide this feature as an
extension if they so choose."

So, what you're asking for is a new feature; its absence is not a bug.

> Compare to correct behavior of printf(1) (/usr/bin/printf) with same args
>   $ /usr/bin/printf "%.*s\n" 5 "Sample string"
>   Sampl

On HP-UX 10.20:

imadev:~$ /usr/bin/printf "%.*s\n" 5 "Sample string"
printf:  Error printing 5

printf:  Error printing Sample string

> printf(1): 
>   "Interpreted sequences are: ...
>   "and all C format specifications ending with one of diouxXfeEgGcs, ... 
>   "Variable widths are handled."

That's the GNU implementation, which has apparently added the variable
width feature.  Bash hasn't added it (yet?).

reply via email to

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