Nirav Gadhiya Nirav Gadhiya - 6 months ago 31
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:

#!/bin/bash

var1=“0x0001”
var2=“0x0005”
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?

Answer
$ 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
15
$ printf '%x\n' $var3
f
$ var3=$(printf '%x' $var3)
$ echo $var3
f
Comments