Handling arguments in specified order in /usr/bin/printf or Bash printf

You can't with bash. the POSIX specification of the printf utility doesn't support it either. You'd have to re-order the arguments by hand.

The printf (or print -f) builtins of ksh93 and zsh support them though:

$ printf '%2$s%1$s\n' a b
ba

GNU awk or perl also support it, so if you have any of those installed, in bash, you could redefine printf as a function like:

printf() { zsh -c 'printf "$@"' printf "$@"; }

or:

printf() { ksh93 -c 'printf "$@"' printf "$@"; }

It would be more effort with gawk or perl however as gawk won't let you pass ARGV as is and neither gawk nor perl would expand the \x sequences (unless passed literally in their code in double quotes) and they don't support %b (an extension of the printf utility used to emulate the SysV echo).

Tags:

Printf

Bash