Shell : Convert Hexadecimal to Decimal

How do I convert hex number to decimal number using a shell script under UNIX / Linux operating systems?

Hexadecimal (hex) is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen.

bc – An arbitrary precision calculator language

There is no need to write a shell script. You can simply use the following syntax at a shell prompt to convert hex to decimal number or vice versa:

echo "obase=16; hex-number"|bc
echo "obase=16; 100"|bc

Sample output:

64
echo "obase=10; decimal-number"|bc
echo "obase=10; AC"|bc

Sample output:

99

ibase and obase define the conversion base for input and output numbers under bc. The default for both input and output is base 10. Add following function to your ~/.bashrc:

d2h(){
echo "obase=16; $@"|bc
}
h2d(){
echo "obase=10; $@"|bc
}

The above two functions can be used from the command line as follows:
$ h2d 100
$ d2h AC

Please re-login to make it works. Bingo !