%*s format specification consumes two arguments like in C, one for the padding width and one for the string to be padded.
printf utility keeps reusing the format if there are arguments remaining after the previous rounds have consumed them:
That's to be used for instance as:
$ printf '%*s%c' 3 x1 '|' 4 y1 '|' 3 z1 "$NL" \
3 x2 '|' 4 y22 '|' 3 z2 "$NL"
x1| y1| z1
x2| y22| z2
To format output in columns. Now, for what's expected as those width arguments, the behaviour depends on the
- For the builtin of
zsh and AT&T
ksh, that can be any arithmetic expression (like in most places that expect a number). So,
1+1 or 2 mean the same.
A as an arithmetic expression resolves to the value of
$A, or 0 is
$A is unset or empty.
printf and the
printf builtin of
dash only accept decimal, octal or hexadecimal literal constants (so 020, 16 and 0x10 mean the same) with leading (but not trailing) blanks ignored and return an error message (but otherwise use whatever valid number if found at the start of the argument or 0 if none) if it's not recognised as a valid number.
printf builtin currently doesn't support
printf '%*s%c' A B C
$A contains an arithmetic expression that resolves to a number with an integer part other than 0.
$ A=5.2*2 zsh -c "printf '%*s%c' A B C"
dash/GNU but also an error message as
A is not a valid decimal/octal/hexadecimal literal constant.
ksh could output error messages if the arithmetic expression is invalid, and like everywhere an arithmetic expression is evaluated, could also run arbitrary commands with the wrong variables in the environment:
$ A=1+ zsh -c "printf '%*s%c' A B C"
zsh:1: bad math expression: operand expected at end of string
$ A='psvar[0$(uname>&2)]' zsh -c "printf '%*s%c' A B C"