Here are the simple steps on how to add/subtract/multiply/divide two numbers in bash script.
1. Create a new file as mentioned below.
vi sum.sh
2. Copy and paste below simple Bash script that calculates the output of 2 numbers :
#!/bin/bash echo "Enter the first number :" read a; echo "Enter the second number :" read b; c=`expr $a + $b` echo "The sum of $a and $b is $c"
Here’s a breakdown of the script:
#!/bin/bash : This line specifies the Bash interpreter.
echo “Enter the first number :” : Asking the user to enter first number.
read a; : Read and store the entered first number using “a” variable.
echo “Enter the second number :” : Asking the user to enter second number.
read b; : Read and store the entered second number using “b” variable.
c=`expr $a + $b` : Add the two numbers using expr(which allows the processing of equations) and using ` gave the output of 2 numbers to variable “c”.
echo “The sum of $a and $b is $c” : Display the final output to user.
3. Save and close the “vi” editor by pressing “Esc” key and type :wq!
4. Make sure to give execute permissions to the script before running it. You can do this by running the following command in the terminal:
chmod +x sum.sh
Here is the final output of the bash script:
In the similar way, you can use subtraction, multiplication and division, but for multiplication, you have to use expr 12 \* 34213.
Below table shows what are all the operators which can be used along with expr command, all the operators will work as expected in command line, however, there will be slight modifications has to be made while using inside the bash script as said for multiplication.
Operator | Description |
ARG1 | ARG2 | Return ARG1 if neither argument is null or zero; otherwise, return ARG2. |
ARG1 & ARG2 | Return ARG1 if neither argument is null or zero; otherwise, return 0. |
ARG1 < ARG2 | Return 1 if ARG1 is less than ARG2; otherwise, return 0. |
ARG1 <= ARG2 | Return 1 if ARG1 is less than or equal to ARG2; otherwise, return 0. |
ARG1 = ARG2 | Return 1 if ARG1 is equal to ARG2; otherwise, return 0. |
ARG1 != ARG2 | Return 1 if ARG1 is not equal to ARG2; otherwise, return 0. |
ARG1 >= ARG2 | Return 1 if ARG1 is greater than or equal to ARG2; otherwise, return 0. |
ARG1 > ARG2 | Return 1 if ARG1 is greater than ARG2; otherwise, return 0. |
ARG1 + ARG2 | Return the arithmetic sum of ARG1 and ARG2. |
ARG1 – ARG2 | Return the arithmetic difference of ARG1 and ARG2. |
ARG1 * ARG2 | Return the arithmetic product of ARG1 and ARG2. |
ARG1 / ARG2 | Return the arithmetic quotient of ARG1 divided by ARG2. |
ARG1 % ARG2 | Return the arithmetic remainder of ARG1 divided by ARG2. |
STRING : REGEXP | Return the pattern match if REGEXP matches a pattern in STRING. |
match STRING REGEXP | Return the pattern match if REGEXP matches a pattern in STRING. |
substr STRING POS LENGTH | Return the substring LENGTH characters in length, starting at position POS |
index STRING CHARS | Return position in STRING where CHARS is found; otherwise, return 0. |
length STRING | Return the numeric length of the string STRING |
`+ TOKEN | Interpret TOKEN as a string, even if it’s a keyword |
(EXPRESSION) | Return the value of EXPRESSION |
Click Here!!! to know how to edit files using VIM editor in linux.
How do you feel about this post? Drop your comments below..