Shell Scripting Basics: How to Add Two Numbers
Welcome to your first step in shell script arithmetic! If you're new to scripting, this is one of the best "first" programs to write. It teaches you two of the most important concepts: how to get input from a user and how to perform a calculation.
Today, we'll write the simplest, most fundamental math script: a program that asks you for two numbers and then tells you the sum.
Let's get started!
The Shell Script
Here is the complete, commented script. It's short, simple, and easy to understand.
#!/bin/bash
# This is a simple script to add two numbers.
# 1. Ask the user for the first number
# 'read -p' lets us print a "prompt" and read the input on one line
read -p "Enter the first number: " num1
# 2. Ask the user for the second number
read -p "Enter the second number: " num2
# 3. Calculate the sum
# We use the $((...)) syntax for arithmetic in Bash.
sum=$(($num1 + $num2))
# 4. Print the final result
echo "------------------------------"
echo "The sum of $num1 and $num2 is: $sum"
How It Works: A Step-by-Step Breakdown
Let's break down each key part of that script.
#!/bin/bash
This is the "shebang." It must be the very first line. It tells your computer's terminal which shell (in this case,bash) to use to run the script.# This is a comment
Any line starting with a#is a comment. The computer ignores it. It's just for humans to leave notes and explain what the code does.read -p "Enter the first number: " num1
This is a compound command:read: This command waits for the user to type something and press Enter.-p: This "prompt" flag lets us show a message to the user before waiting for their input."...": This is the prompt string that gets displayed.num1: This is the variable. Whatever the user types is stored inside this variable.
sum=$(($num1 + $num2))
This is the most important line! This is how you do math in Bash.$((...)): This is called "Arithmetic Expansion." It's the modern, easy way to do math in Bash.$num1 + $num2: Inside the$(( )), we can use our variables (with the$) and standard math symbols like+,-,*, and/.sum=...: We store the result of the calculation in a new variable calledsum.
echo "The sum of $num1 and $num2 is: $sum"
Theechocommand prints text to the screen. When you put a$in front of a variable name (like$sum), the shell replaces it with the value stored inside that variable.
How to Run Your Script
Ready to try it? Here's how to run it in your terminal.
Step 1: Create the file
Use a text editor like nano to create your script file:
nano add.sh
Step 2: Paste the code
Copy the entire shell script from the first section and paste it into the nano editor.
Step 3: Save and Exit
(In nano: Press Ctrl+O, Enter to save. Press Ctrl+X to exit.)
Step 4: Make the script executable
You need to give the file permission to be run as a program. We use chmod (change mode) for this.
chmod +x add.sh
Step 5: Run it!
Execute your script by typing its name:
./add.sh
Example Output
When you run the script, it should look just like this:
$ ./add.sh
Enter the first number: 10
Enter the second number: 15
------------------------------
The sum of 10 and 15 is: 25
Run it again and try different numbers!
$ ./add.sh
Enter the first number: 123
Enter the second number: 456
------------------------------
The sum of 123 and 456 is: 579
Great Job! What's Next?
You've just built a basic calculator! You've learned how to get input, store it in variables, and perform arithmetic.
Challenge: Try modifying this script to perform subtraction instead. (Hint: You'll just need to change one symbol!)
Happy scripting!

0 Comments