Reputation: 1158
Can someone explain me this bash code?
read text && echo $text | bc -l
Thanks!
Upvotes: 1
Views: 216
Reputation: 3848
To be brief: read user input, pass it to bc
command and output its result.
More on bc command: http://www.gnu.org/software/bc/manual/html_mono/bc.html
Step by step explanation:
read text && echo $text | bc -l
read text
Reads a line from the standard input and saves it in the text
variable.
&&
Lets you do something based on whether the previous command completed successfully.
echo
Display message to the standar output.
$text
Retrieves the value of the text
variable.
|
A pipe lets you use the output of a program as the input of another one
bc -l
bc is an arbitrary precision calculator language.
-l is an option defining the standard math library.
So, the full code:
text
text
variable to bc
bc
will return the result.Usage:
$ read text && echo $text | bc -l
(3+3)*2 <-- user input containing a mathematical expression
12 <-- result
Upvotes: 6