bash script on padding 0 on digit with character

Viewed 55

I want to write a bash scritpt where it will put 0 padding infront of a hexadecimal character eg. 32b, ae2, etc. it works on just integer but not on mix character and digit. I used

printf "%04d$a\n" .  a=ae2.   

what I want to happen is like this 0ab3, 003s, 000a, 002a padding

1 Answers

I would suggest this:

printf '%04x\n' "0x${a}"

x in format specification stands for hexadecimal integer (lowercase). And without 0x prefix in argument bash will complain that ae2 is an invalid number.

Related