Expr and calc command: Solve mathematical expressions in terminal

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

Note that before the asterisk * I put the backslash - »\ ... it is necessary, because expr does not interpret the asterisk as a multiplication symbol unless the \

And the result will appear on the screen. Here is a screenshot with some examples:

screenshot_expr1

As you can see in the final calculations (14/4 and 13/4), 14 divided by 4 is actually 3.5 and 13 divided by 4 is actually 3.25, so how is it possible that it shows us 3 in both cases? What happens is that the expr command does not show us the decimals, that is, what follows after the comma, it does not show it, it only shows us the integer.

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

It is important that there is a space between each character, that is, a space between each number, each addition, subtraction, multiplication or division symbol

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:

screenshot_calc

Unlike expr, when we use calc it is better that there is NO space between each character, that is, that there are no spaces between numbers and symbols, just as you see in the previous image. Nor do they need to prepend a \ before the *

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


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   elav said

    Very useful, although it would definitely not be good to use Bash for math calculations, decimals count :)

    1.    Wada said

      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

      1.    elav said

        😀

      2.    eliotime3000 said

        Excellent. This way, I avoid the hassle of installing XCalc when using pure X11.

      3.    KZKG ^ Gaara said

        Oh I didn't know this one, great !!

  2.   clow_eriol said

    Thanks, it's quite useful 😉

    1.    KZKG ^ Gaara said

      Thanks ^ _ ^

  3.   Noctuid said

    Thank you. The system calculator has been a competitor for basic operations.

    1.    KZKG ^ Gaara said

      Thanks to you for reading us 🙂

  4.   Joaquin said

    Good tip!

  5.   Pedro lala said

    I did not like