5 Helpful Methods to Do Arithmetic in Linux Terminal
On this article, we are going to present you varied helpful methods of doing arithmetic’s within the Linux terminal. By the top of this text, you’ll be taught fundamental completely different sensible methods of doing mathematical calculations within the command line.
Let’s get began!
1. Utilizing Bash Shell
The primary and easiest method do fundamental math on the Linux CLI is a utilizing double parenthesis. Listed below are some examples the place we use values saved in variables:
$ ADD=$(( 1 + 2 ))
$ echo $ADD
$ MUL=$(( $ADD * 5 ))
$ echo $MUL
$ SUB=$(( $MUL – 5 ))
$ echo $SUB
$ DIV=$(( $SUB / 2 ))
$ echo $DIV
$ MOD=$(( $DIV % 2 ))
$ echo $MOD
Arithmetic in Linux Bash Shell
2. Utilizing expr Command
The expr command evaluates expressions and prints the worth of supplied expression to plain output. We’ll have a look at other ways of utilizing expr for doing simple arithmetic, making comparability, incrementing the worth of a variable and discovering the size of a string.
The next are some examples of doing easy calculations utilizing the expr command. Be aware that many operators should be escaped or quoted for shells, for example the * operator (we are going to have a look at extra underneath comparability of expressions).
$ expr three + 5
$ expr 15 % three
$ expr 5 * three
$ expr 5 – three
$ expr 20 / four
Primary Arithmetic Utilizing expr Command in Linux
Subsequent, we are going to cowl the right way to make comparisons. When an expression evaluates to false, expr will print a price of zero, in any other case it prints 1.
Let’s have a look at some examples:
$ expr 5 = three
$ expr 5 = 5
$ expr eight != 5
$ expr eight > 5
$ expr eight < 5
$ expr eight <= 5
Evaluating Arithmetic Expressions in Linux
You can even use the expr command to increment the worth of a variable. Check out the next instance (in the identical means, you can too lower the worth of a variable).
$ NUM=$(( 1 + 2))
$ echo $NUM
$ NUM=$(expr $NUM + 2)
$ echo $NUM
Increment Worth of a Variable
Let’s additionally have a look at the right way to discover the size of a string utilizing:
$ expr size “That is Tecmint.com”
Discover Size of a String
For extra data particularly on the which means of the above operators, see the expr man web page:
$ man expr
three. Utilizing bc Command
bc (Primary Calculator) is a command-line utility that gives all options you anticipate from a easy scientific or monetary calculator. It’s particularly helpful for doing floating level math.
If bc command not put in, you possibly can set up it utilizing:
$ sudo apt set up bc #Debian/Ubuntu
$ sudo yum set up bc #RHEL/CentOS
$ sudo dnf set up bc #Fedora 22+
As soon as put in, you possibly can run it in interactive mode or non-interactively by passing arguments to it – we are going to have a look at each case. To run it interactively, kind the command bc on command immediate and begin doing a little math, as proven.
$ bc
Begin bc in Non-Interactive Mode
The next examples present the right way to use bc non-interactively on the command-line.
$ echo ‘three+5′ | bc
$ echo ’15 % 2′ | bc
$ echo ’15 / 2’ | bc
$ echo ‘(6 * 2) – 5’ | bc
Do Math Utilizing bc in Linux
The -l flag is used to the default scale (digits after the decimal level) to 20, for instance:
$ echo ’12/5 | bc’
$ echo ’12/5 | bc -l’
Do Math with Floating Numbers
four. Utilizing Awk Command
Awk is likely one of the most distinguished text-processing packages in GNU/Linux. It helps the addition, subtraction, multiplication, division, and modulus arithmetic operators. It is usually helpful for doing floating level math.
You should utilize it to do fundamental math as proven.
$ awk ‘BEGIN ‘
$ awk ‘BEGIN a = 6; b = 2; print “(a – b) = “, (a – b) ‘
$ awk ‘BEGIN ‘
$ awk ‘BEGIN a = 6; b = 2; print “(a / b) = “, (a / b) ‘
$ awk ‘BEGIN a = 6; b = 2; print “(a % b) = “, (a % b) ‘
Do Primary Math Utilizing Awk Command
If you’re new to Awk, now we have an entire collection of guides to get you began with studying it: Be taught Awk Textual content Processing Device.
5. Utilizing issue Command
The issue command is use to decompose an integer into prime components. For instance:
$ issue 10
$ issue 127
$ issue 222
$ issue 110
Issue a Quantity in Linux
That’s all! On this article, now we have defined varied helpful methods of doing arithmetic’s within the Linux terminal. Be at liberty to ask any questions or share any ideas about this text through the suggestions type beneath.