Nirav Gadhiya Nirav Gadhiya - 1 year ago 65
Bash Question

Shell Script: value too great for base (error token is "16#?")

I am new to shell script. I am working with Hex values and writing a simple script for substraction. Here is my script:


var3=“$(( 16#$var2 - 16#$var1 ))”
echo “Diference $var3”

I am getting this error :

line 6: 16#?: value too great for base (error token is "16#?")

Could you please let me know where my mistake is?

$ var1=0x0001
$ var2=0x0005
$ var3=$(( $var2 - $var1 ))
$ echo "Diference $var3"
Diference 4

Assign the hex values without double quotes(i.e not as strings).
Since you have already put a 0x there is no need for 16#

To conver the answer back to hex you can use:

printf '%x' $num

Here is an example:

$ var1=0x19
$ var2=0xA
$ var3=$(( $var1 - $var2 ))
$ echo $var3
$ printf '%x\n' $var3
$ var3=$(printf '%x' $var3)
$ echo $var3