"Invalid Arithmetic Operator" in Shell

Invalid Arithmetic Operator when doing floating-point math in bash

bash does not support floating-point arithmetic. You need to use an external utility like bc.

# Like everything else in shell, these are strings, not
# floating-point values
d1=0.003
d2=0.0008

# bc parses its input to perform math
d1d2=$(echo "$d1 + $d2" | bc)

# These, too, are strings (not integers)
mean1=7
mean2=5

# $((...)) is a built-in construct that can parse
# its contents as integers; valid identifiers
# are recursively resolved as variables.
meandiff=$((mean1 - mean2))

bash if invalid arithmetic operator

You are comparing a string with a numeric value. The syntax in the below if condition is to compare two numeric value and not for string comparison. Also your if condition is not properly ended with ]]. I thing you are comparing a string with a numeric value. Please see below correct if conditions:-

First a correct if condition if both the right and left hand values are numeric :-

if [[ ( $nameserver -eq $value1 ) || ( $nameserver -eq $value2 ) ]]

For correct string comparison:-

if [[ ( "$nameserver" == "$value1" ) || ( "$nameserver" == "$value2" ) ]]

Hope this will help you.

Bash invalid arithmetic operator on shell script

When Bash creates this error message it does something very similar to `printf 'syntax error: invalid arithmetic operator (error token is "%s")\n' "$token". For that to end up as

")syntax error: invalid arithmetic operator (error token is "

(the 1 lines are from your echos) your file must contain a carriage return (\r) character, so it ends up printing syntax error: invalid arithmetic operator (error token is ", then moving to the start of the line (which is what carriage return does), and finally printing ") there.

To make this work you should extract the current version number from the file, printf '%s' "$version" > versioncode.txt to clean up the file, and use that command in your script (instead of echo) to ensure the format of that file.

invalid arithmetic operator in shell

Declare the array variable as an associative array with declare -A arr.

$ cat test.sh 
#!/bin/bash
set -x
declare -A arr
key="index.index";
arr["$key"]="val"
echo "${arr["${key}"]}"

$ ./test.sh
+ declare -A arr
+ key=index.index
+ arr["$key"]=val
+ echo val
val

How to handle errors with Bash arithmetic operations?

If the intention is just to discard the error message, then the natural way to try to do it is

echo $(( 3.5 + 1)) 2>/dev/null

Unfortunately, that doesn't work. I guess it's because the error message is coming from Bash itself. However, this does work (at least on Bash 4.2):

( echo $(( 3.5 + 1)) ) 2>/dev/null

The (...) creates a subshell, which is very expensive on some platforms (particularly Cygwin and WSL). This slightly messier code also works, and doesn't create a subshell:

{ echo $(( 3.5 + 1)) ; } 2>/dev/null

Syntax error: invalid arithmetic operator in bash scripting

Make sure that you are actually running in bash shell.

#!/bin/bash
a=0.32; b=0.18; c=0.60
if [[ $(bc -l <<< "$a > $b") -eq 1 ]]; then
echo "$a > $b"
else
echo "$a <= $b"
fi

if [[ $(bc -l <<< "$a > $c") -eq 1 ]]; then
echo "$a > $c"
else
echo "$a <= $c"
fi


Related Topics



Leave a reply



Submit