Error in Bash script arithmetic syntax

Script:

#!/bin/bash vpct=5.3 echo $((vpct*15)) 

Error:

 ./abc.sh: line 5: 5.3: syntax error: invalid arithmetic operator (error token is ".3") 

I know that I do not need a script to multiply 5.3 * 15 , but this little script to highlight the error. Please inform.

+4
source share
6 answers

Besides bc , there are other tools you can use to try

 awk -v vpct="$VPCT" 'BEGIN{print vpct * 15}' echo $vpct | ruby -e 'print gets.to_f * 15 ' echo "$vpct 15 * p" | dc 
+3
source

According to http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml :

Bash does not understand floating point arithmetic. It processes numbers containing a decimal point as strings.

You must use bc to perform calculations such as in the dogbane solution, except that you must avoid using quotation marks so that the * character does not cause unwanted shell extensions.

 echo "$vpct*15" | bc 
+7
source

You should use bc for floating point arithmetic:

 echo "$vpct*15" | bc 
+3
source

$ (($ vpct * 15)) // (add the $ sign to do this)

0
source

Shebang should be written as #! And still, $(()) is for integers only.

0
source

If you have ksh, it will do the float arithmetic.

0
source

Source: https://habr.com/ru/post/1340303/


All Articles