Hexadecimal To Decimal in Shell Script Hexadecimal To Decimal in Shell Script bash bash

Hexadecimal To Decimal in Shell Script


To convert from hex to decimal, there are many ways to do it in the shell or with an external program:

With :

$ echo $((16#FF))255

with :

$ echo "ibase=16; FF" | bc255

with :

$ perl -le 'print hex("FF");'255

with :

$ printf "%d\n" 0xFF255

with :

$ python -c 'print(int("FF", 16))'255

with :

$ ruby -e 'p "FF".to_i(16)'255

with :

$ nodejs <<< "console.log(parseInt('FF', 16))"255

with :

$ rhino<<EOFprint(parseInt('FF', 16))EOF...255

with :

$ groovy -e 'println Integer.parseInt("FF",16)'255


Dealing with a very lightweight embedded version of busybox on Linux means many of the traditional commands are not available (bc, printf, dc, perl, python)

echo $((0x2f))47hexNum=2fecho $((0x${hexNum}))47

Credit to Peter Leung for this solution.


One more way to do it using the shell (bash or ksh, doesn't work with dash):

echo $((16#FF))255