Home > Mobile >  Why do I get "syntax error: invalid arithmetic operator" in a line with a valid syntax
Why do I get "syntax error: invalid arithmetic operator" in a line with a valid syntax

Time:12-15

This is the snippet of my code

valueSum="$(($firstValueBinarytoDecimal $secondValueBinarytoDecimal))"
                        decimalToBinary="$(("obase=2;$valueSum" | bc))" #error here
                        echo "$decimalToBinary"

On the marked code: decimalToBinary="$(("obase=2;$valueSum" | bc))", I get an error message: syntax error: invalid arithmetic operator (error token is ";150 | bc")

I've already run the exact same code separately and it's working

echo "obase=2;150" | bc

I thought maybe it could be that the 150 was acting as a string and not as an intager but then why I'm able to make the addition $(($firstValueBinarytoDecimal $secondValueBinarytoDecimal))?

Somebody knows how can I fix this error?

CodePudding user response:

This is the corrected form of your line:

decimalToBinary="$( echo "obase=2;$valueSum" | bc )"

  • Related