I wanted to do this calculation a * 256 ** 3 + b * 256 ** 2 + c * 256 + d and assign output to variable and print dec.
dec ='a * 256 ** 3 + b * 256 ** 2 + c * 256 + d'
echo $dec
I am getting syntax error with the above lines.
I wanted to do this calculation a * 256 ** 3 + b * 256 ** 2 + c * 256 + d and assign output to variable and print dec.
dec ='a * 256 ** 3 + b * 256 ** 2 + c * 256 + d'
echo $dec
I am getting syntax error with the above lines.
First of all, assignment is done this way
This is where your syntax error come from. Then you just have to evaluate your string with the operator $(( )) this way :
For integer arithmetic, you can use the shell:
This uses an arithmetic context
(( ... ))
to calculate the value.Note that there is no space in an assignment, this is important!
The shell does not support floating point arithmetic, but
bc
orawk
do. For example, using awk (and assuming that you have shell variables$a
,$b
,$c
and$d
defined):or using bc:
Using the string format specifier
%s
for each of the shell variables means that no precision is lost before passing the values to bc.