Sqrt issue in bash

guillermo -  
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   -
Hello,
right now, I am working on a bash program and I need to calculate a square root.
For that, I am using the command sqrt:
to calculate the square root of the variable "$a" and store it in the variable "$rac", I do the following: $rac = sqrt($a)

But when I run the script, I get the following message:
./essai2.sh: line 14: Syntax error near unexpected token « ( »
./essai2.sh: line 14: '$rac = sqrt($a)'

Thank you in advance.

Configuration: Linux / Firefox 3.6.13

2 answers

KarCAroum Posted messages 298 Status Member 91
 
Good evening,
rac=$(bc -l <<< "sqrt($a)") (corrected)
2
zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
 
Hi,

A variable in bash is declared without the "$" sign (dollar) and without spaces before or after the "=" sign (equal) ;-((

rac=$(bc -l <<<"sqrt($a)")
0
KarCAroum Posted messages 298 Status Member 91
 
certainly but the essential is bc -l <<<"sqrt($a)"
0
zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
 
Sure, but the comment was just in the interest of our friend guillermo ;-))
1
KarCAroum Posted messages 298 Status Member 91
 
she is fully justified
0
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
Hello,

@zipe31

It reminds me of a message ;-)
0
guillermo
 
Thank you very much, it works. But I would like to understand why we have to use: "bc -l".
0
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
Re,

Try it without -l and you'll see
Look at man bc too
0