Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bc in bash script

Tags:

bash

bc

I am trying outputing result in floating point using bc in bash.But I am getting the following output for the following code.How can i get the multiplication result from here and also why i am getting command not found.

 #!/bin/bash 
 v1=3.41 
 v2=45 
 v3= $(bc <<< "scale=4;$v1 + $v2")
 echo $v3 
 v3= $(bc <<< "scale=4;$v1 - $v2") 
 echo $v3 
 v3= $(bc <<< "scale=4;$v1 / $v2") 
 echo $v3 
 v3= $(bc <<< "scale=4;$v1 % $v2") 
 echo $v3
 v3 = $(bc <<< "scale=4;$v1 * $v2") 
 echo $v3 

the output i am getting is below :

mint@mint ~ $ bash bc.sh 
bc.sh: line 4: 48.41: command not found

bc.sh: line 6: -41.59: command not found

bc.sh: line 8: .0757: command not found

bc.sh: line 10: .0035: command not found

bc.sh: line 12: v3: command not found
like image 323
user3708629 Avatar asked Oct 14 '25 09:10

user3708629


1 Answers

Whitespace does matter. Remove it.

v3= $(bc <<< "scale=4;$v1 + $v2")
   ^

Explanation: The following command runs app with a locally exported var with a value value:

var=value app

In your case value is empty.

like image 96
Karoly Horvath Avatar answered Oct 17 '25 01:10

Karoly Horvath