How to force decimal interpretation of printf's parameter in Bash instead of octal

2.1k views Asked by At

I am formatting numbers in a bash script with printf

printf "%04d" $input   # four digits width with leading zeros if shorter

If input="011" printf assumes the value is octal and returns 0009 instead of 0011.

Is it possible to force printf to assume all values are decimal?

1

There are 1 answers

2
chepner On BEST ANSWER

Prefix the value with a base specifier of 10#.

$ printf "%04d\n" "$input"
0009
$ printf "%04d\n" "$((10#$input))"
0011