6

I have a decimal number in the bash shell:

linux$ A=67

How do I print 67 as hexadecimal in bash?

Benjamin W.
  • 38,596
  • 16
  • 96
  • 104
MOHAMED
  • 38,769
  • 51
  • 148
  • 252
  • 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

26

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
  • 135,913
  • 89
  • 401
  • 325
Vorsprung
  • 30,988
  • 4
  • 36
  • 58