4

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 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool :-) Nice mentioning about the 0x prefix.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.