Hello everybody,
I would like to understand why the printf function is returning me an octal value with this command :
printf %4.4d 0010 returns 0008
printf %4.4d 10 returns 0010
Thanks for help.
Hello everybody,
I would like to understand why the printf function is returning me an octal value with this command :
printf %4.4d 0010 returns 0008
printf %4.4d 10 returns 0010
Thanks for help.
Because of the '0' at the beginning. This usually tells the system to interpret this as an octal value, much like 0x starts a hex number.
Allright, but removing the zeros is not the solution i m looking for.
Is there a way to tell printf to interpret it as decimal value ?
The obvious question, then, is: why not?
$ var1="0011"
$ echo $var1
0011
$ echo $var1 | sed -e 's/^0\+//'
11
$ printf "%04d\n" $( echo $var1 | sed -e 's/^0\+//' )
0011
$ var2=$( printf "%04d\n" $( echo $var1 | sed -e 's/^0\+//' ) )
$ echo $var2
0011
$ var2=$( printf "%05d\n" $( echo $var1 | sed -e 's/^0\+//' ) )
$ echo $var2
00011
Pretty simple, isn't it? Other than that (or using bc instead of sed) I know no way to "force" printf on how to interpret the argument.