0

Can anyone please tell me how I should go about assigning the value of the square root of a number to a variable in Bash?

This is my code.

#!/bin/bash
a=5
c=sqrt"($a)"|bc -l
echo "$c"

When executed, it displays nothing

However, I observe that when the following is executed,

#!/bin/bash
a=5
echo sqrt"($a)"|bc -l

I get an answer

2.23606797749978969640

Can someone tell me as to how I could bind the value of the square root of a to c?

Thanks.

Viswa
  • 316
  • 1
  • 14

2 Answers2

0
c=$(bc -l <<< "sqrt($a)")

or

c=$(echo "sqrt($a)" | bc -l)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You want to take the first string and pipe it through bc putting the result in c. The following works (as mentioned by John Kugelman):

#!/bin/bash
a=5
c=$(echo sqrt"($a)" | bc -l)
echo "$c"
HackerBoss
  • 829
  • 7
  • 16