I am one of those who are always programming scripts bash to solve day-to-day situations (crack the AP / Router password, automate processes, etc.).
On certain occasions I have seen the need to assign the value of a mathematical expression to a variable, or simply work with mathematical expressions, there I find myself with the problem that bash and mathematical expressions, calculations are not very friendly. It happens that bash is not like python, what can we say variable = 1 + 5/6 (for example), in Bash we must use the command expose o tracing
Expr command
In other words, the expr command helps us to solve mathematical expressions in the terminal, with it we can calculate what we need. For example, if we want the result of 1 + 2 * 8/3 to appear, we put in the terminal:
expr 1 + 2 \* 8 / 3
And the result will appear on the screen. Here is a screenshot with some examples:
In case we want to assign to a variable the result of a mathematical expression (Ex: 10/2), it would be like this:
variable=`expr 10 / 2`
Then we check it with:
echo $variable
Calc command
This one, unlike the previous one, is exact in terms of decimals, example:
calc 15 / 4
It would show us: 3.75
Here is a screenshot with several examples:
Well, this is basically what I wanted to tell you about.
However, there are still some other interesting tips for each command (especially calc), I recommend that you read the manual:
man calc
man expr
regards
Very useful, although it would definitely not be good to use Bash for math calculations, decimals count :)
Mr. elav we still have bash calculator 🙂 we can use the -l argument to use the standard math library
something like that
$ echo "(4/8)+(6/9)" | bc -l
1.166666666666666666666666
ha
😀
Excellent. This way, I avoid the hassle of installing XCalc when using pure X11.
Oh I didn't know this one, great !!
Thanks, it's quite useful 😉
Thanks ^ _ ^
Thank you. The system calculator has been a competitor for basic operations.
Thanks to you for reading us 🙂
Good tip!
I did not like