Bash Scripting for Beginners (Part 3— Input and Arithmetic Operations)
Let’s continue with input and arithmetic operations.

Input
If we would like to ask the user for input then we use read
command.This command takes the input and will save it into a variable.
Example:
#!/bin/bashecho What is your name?
read name
echo Nice to meet you, $name!
Output:
$ ./input.sh
What is your name?
Okan
Nice to meet you, Okan!
There are two options of read
command. -p
option which allows you to specify a prompt and -s
which makes the input silent. Look at the example:
#!/bin/bashread -p 'Username: ' username
read -sp 'Password: ' password
echo Welcome $username, now you logged in!
We can get multiple entries at the same time.
#!/bin/bashecho What is your name?
read n1 n2 n3
echo Your first name: $n1
echo Your second name: $n2
echo Your last name: $n3
Output:
$ ./input.sh
What is your name?
Ahmet Okan YILMAZ
Your first name: Ahmet
Your second name: Okan
Your last name: YILMAZ
Arithmetic Operations
Bash can be used to perform calculations. It follows the basic format:
let <arithmetic expression>
Here is a table with the most commonly used operators:

#!/bin/bash
# bash addition
let ADDITION=3+5
echo "3 + 5 =" $ADDITION
# bash subtraction
let SUBTRACTION=7-8
echo "7 - 8 =" $SUBTRACTION
# bash multiplication
let MULTIPLICATION=5*8
echo "5 * 8 =" $MULTIPLICATION
# bash division
let DIVISION=4/2
echo "4 / 2 =" $DIVISION
# bash modulus
let MODULUS=9%4
echo "9 % 4 =" $MODULUS
# bash power of two
let POWEROFTWO=2**2
echo "2 ^ 2 =" $POWEROFTWO
We can write same example with double parentheses.
#!/bin/bash
echo 3 + 5 = $((3 + 5))
echo 7 - 8 = $((7 - 8))
echo 5 x 8 = $((5 * 8))
echo 4 / 2 = $((4 / 2))
echo 9 % 4 = $((9 % 4))
echo 2 ^ 2 = $((2 ** 2))
Or:
#!/bin/bash
echo 3 + 5 = $[ 3 + 5 ]
echo 7 - 8 = $[ 7 - 8 ]
echo 5 x 8 = $[ 5 * 8 ]
echo 4 / 2 = $[ 4 / 2 ]
echo 9 % 4 = $[ 9 % 4 ]
echo 2 ^ 2 = $[ 2 ** 2 ]
expr
is similar to let
except instead of saving the result to a variable it instead prints the answer.
#!/bin/bashexpr 2 + 2
expr 2+2
expr "2 + 2"
Output:
$ ./ops.sh
4
2+2
2 + 2
We will continue in the next article.