Printf behavior on AIX

command:

$ printf 'u1u1.%*s' 7

so if i run the above command on a linux box, i get the expected output of:

jserver@jserver-VirtualBox:~$ printf 'u1u1.%*s' 7
u1u1.       jserver@jserver-VirtualBox:~$ 
jserver@jserver-jserver:~$ 

it puts 7 spaces after the u1u1.

but when i run it on AIX or older systems, i get this:

$ printf 'u1u1.%*s' 7
printf: bad conversion
u1u1.s$ 

does anyone know of any command that will work across on UNIX platforms?

There is no operand supplied for the %s conversion. Try:

$ printf 'u1u1.%*s' 7 ""

which should work with any printf utility.

i tried that. it didn't work:

$ printf 'u1u1.%*s' 7 ""
printf: bad conversion
printf: bad conversion
u1u1.su1u1.s$ 
$ 

I apologize. The POSIX standards do specify the meaning of * in %*s in the printf family of functions ( dprintf , fprintf , printf , snprintf , sprintf , fwprintf , swprintf , and wprintf ) in the format string argument and in the awk printf and sprintf functions format string argument; but it does not specify the meaning of the * in %*s in the printf utility format string operand.

On OS X El Capitan (Version 10.11.5), the ksh and bash printf built-ins and the /usr/bin/printf stand-alone utility all produce the output:

u1u1.x       x

when invoked with the arguments:

printf 'u1u1.x%*sx\n' 7 ""

(which is what would be required by the C and awk printf functions when given those arguments).

Even though it isn't required by the standards, I'm surprised that the AIX printf utility doesn't implement it as a front-end to the C function. Do you get the same results with the stand-alone utility and the shell built-ins on AIX?

1 Like

printf

width=7
printf "u1u1.%${width}s<" ""
u1u1.       <
printf "u1u1.%${width}s<" "xxx"
u1u1.    xxx<
2 Likes