Documentos de Académico
Documentos de Profesional
Documentos de Cultura
In this tutorial, we will learn about performing arithmetic operations in shell scripts & also about
the logical/boolean operators. So let’s first start with the all the arithmetic operations as well as
the arithmetic, boolean operators that can be used in Linux,
Also Read: Bash Scripting-4- Using conditionals (if else in shell script)
* Multiplication == Equal to
|| logical OR
Now let’s see some examples of performing arithmetic operation in shell script,
#!/bin/bash
num1=10
num2=20
num3=40
#!/bin/bash
num1=10
num2=20
num3=40
echo “ Multipying $num1 & $num2, output is $[$num1*$num2]”
Now when we perform some arithmetic operations like division, we might get the answer as
floating point number. For example,
#!/bin/bash
num1=5
num2=2
So the output, in this case, would be ‘2.5’ but we would get the output as ‘2’. So how to work this
out, how to get the right answer. To accomplish this floating point arithmetic operation, we need
to use ‘bc’ command.
# “scale=3; num1/num2 | bc
Here scale=3 means the number of decimal places up to which division will be performed. So
the above mentioned script will change to,
#!/bin/bash
num1=5
num2=2
So with this, we will end this tutorial on performing Arithmetic Operation in shell script. These
bash scripting tutorials will continue with our coming posts as well. Please do send us your
suggestions, questions or queries using the comment box below.
If you think we have helped you or just want to support us, please consider these:-
TheLinuxGURUS.com