Bash script when filling 0 on a digit with a character

I want to write bash scritpt where it will put 0 capital letters in a hexadecimal character, for example. 32b, ae2, etc., It only works with an integer, but not with a mix character and a digit. I used

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

what i want looks like this: 0ab3, 003s, 000a, 002a padding

+4
source share
1 answer

I would suggest the following:

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

xin the format specification, indicates a hexadecimal integer (lowercase). And without a 0xprefix in the argument, bash will complain that it ae2is an invalid number.

+5
source

Source: https://habr.com/ru/post/1689044/


All Articles