operation () {
operator=${exp:1:1} # 2nd character / 1st is ${words:0:1}
# Read into an array as tokens separated by IFS
IFS="$operator"
read -ra array <<< "$exp" # -ra = raw input array
a=array[0]
b=array[1]
# https://www.computerhope.com/unix/bash/read.htm
if [ "$operator" == "+" ]; then
operation=$((a+b))
fi
if [ "$operator" == "-" ]; then
operation=$((a+b)) # => ' 1'
operation=`expr a-b` # => ' 1'
fi
if [ "$operator" == "*" ]; then
operation=$((a*b))
fi
if [ "$operator" == "/" ]; then
operation=$((a/b))
fi
# https://www.tutorialsandyou.com/bash-shell-scripting/bash-arithmetic-operations-11.html
echo $((operation))
}
exp='2-3'
operation $exp
Everything(almost) works fine, just when making the subtraction operation=$((a+b))
or operation=`expr a-b`
I cannot print in anyway minus('-') instad of space(' '), the (-) sign it is been replaced by space. In this example I get ' 1'
instead of '-1'
.
Why is this happening?