How to compare hexadecimal numbers with hexadecimal numbers in shell?

At least bash supports hexadecimal integers directly, provided that they are prefixed with 0x:

$ [[ 0xdead -lt 0xcafe ]] && echo yes || echo no
no
$ [[ 0xdead -gt 0xcafe ]] && echo yes || echo no
yes

You just use the comparison operators normally...


You can convert your hex numbers into decimals using printf and then you can compare the numeric values, e.g.:

x="0xdead"
y="0xcafe"

x_num=$(printf "%d" "$x")
y_num=$(printf "%d" "$y")

if [ $x_num -gt $y_num ]; then
    echo "x is my value"
else
   echo "x is not my value"
fi

Tags:

Shell

Bash