shell script to add two numbers using function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# above line used to specify which shell to be used to execute the program | |
add() # function to add 2 numbers | |
{ | |
num1=$1 | |
num2=$2 | |
sum=`expr $num1 + $num2` # things insed the backtick `are evaluate or executed | |
echo "$num1 + $num2 = $sum" | |
} | |
echo "Enter first Number" # echo used to print the message to screen | |
read first_number # read and store the input from terminal | |
echo "enter second Number" | |
read second_number | |
add $first_number $second_number # call the add function | |
# give execution permission to file before running the program like bellow | |
# chmod +x add2.sh | |
# run the program by ./add2.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment