6

I have a decimal number in the bash shell:

linux$ A=67

How do I print 67 as hexadecimal in bash?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 2
    the printf in bash is a builtin, it does not fork a shell. I have changed my answer to give an example of it directly setting variables as in the question – Vorsprung May 13 '13 at 17:46

1 Answers1

27

As a bash program:

#!/bin/bash 

decimal1=31

printf -v result1 "%x" "$decimal1"

decimal2=33

printf -v result2 "%x" "$decimal2"

echo  $result1 $decimal1
echo  $result2 $decimal2

Or directly from the bash shell:

el@defiant ~ $ printf '%x\n' 26
1a
el@defiant ~ $ echo $((0xAA))
170
el@defiant ~ $ 
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Vorsprung
  • 32,923
  • 5
  • 39
  • 63