📖 Scenario: You want to create a simple bash script that takes two numbers as input when you run it from the command line. Then, the script will add these two numbers and show the result.This is like when you use a calculator app and enter two numbers to get their sum, but here you will type the numbers next to the script name in the terminal.
🎯 Goal: Build a bash script that reads two numbers from the command line arguments $1 and $2, adds them, and prints the sum.
📋 What You'll Learn
Create a bash script that uses command-line arguments
Use $1 and $2 to get the first and second input numbers
Add the two numbers inside the script
Print the result to the terminal
💡 Why This Matters
🌍 Real World
Many command-line tools accept inputs as arguments. Learning to use <code>$1</code>, <code>$2</code>, etc., helps automate tasks and create flexible scripts.
💼 Career
Understanding command-line arguments is essential for system administrators, DevOps engineers, and anyone writing shell scripts to automate workflows.
Progress0 / 4 steps
1
Create a bash script file
Create a bash script file named add_numbers.sh and add the first line #!/bin/bash to specify the script interpreter.
Bash Scripting
Hint
The first line in a bash script should be #!/bin/bash to tell the system to use bash to run the script.
2
Assign command-line arguments to variables
Inside add_numbers.sh, create two variables called num1 and num2 and assign them the values of the first and second command-line arguments $1 and $2 respectively.
Bash Scripting
Hint
Use num1=$1 and num2=$2 to get the first and second inputs from the command line.
3
Add the two numbers
Add the two variables num1 and num2 using arithmetic expansion and store the result in a variable called sum.
Bash Scripting
Hint
Use sum=$((num1 + num2)) to add the two numbers in bash.
4
Print the sum
Print the value of sum to the terminal using echo.
Bash Scripting
Hint
Use echo $sum to show the result on the screen.
Try running the script with ./add_numbers.sh 7 8 to see the output 15.
Practice
(1/5)
1. What does $1 represent in a bash script?
easy
A. The last command-line argument passed to the script
B. The total number of arguments passed to the script
C. The name of the script file
D. The first command-line argument passed to the script
Solution
Step 1: Understand command-line arguments in bash
In bash scripts, $1 refers to the first argument given after the script name.
Step 2: Differentiate from other special variables
$# is the count of arguments, $0 is the script name, and $@ is all arguments.
Final Answer:
The first command-line argument passed to the script -> Option D
Quick Check:
$1 = first argument [OK]
Hint: Remember $1 is always the first argument after script name [OK]
Common Mistakes:
Confusing $1 with $# (argument count)
Thinking $1 is the script name
Mixing $1 with $0 or $@
2. Which of the following is the correct way to access the third argument in a bash script?
easy
A. $0
B. $2
C. $3
D. $#
Solution
Step 1: Recall argument numbering in bash
Arguments start from $1 for the first, so the third argument is $3.
Step 2: Identify incorrect options
$2 is second argument, $# is count, $0 is script name.
Final Answer:
$3 -> Option C
Quick Check:
Third argument = $3 [OK]
Hint: Count arguments starting at $1, so third is $3 [OK]
Common Mistakes:
Using $2 instead of $3 for third argument
Confusing $# with argument value
Using $0 which is script name
3. What will be the output of this script if run as ./script.sh apple banana cherry?
Arguments are apple ($1), banana ($2), cherry ($3). Script name is $0.
Step 2: Understand output commands
echo prints $1 as apple, $2 as banana, and $# as 3 (number of arguments).
Final Answer:
First: apple
Second: banana
Count: 3 -> Option A
Quick Check:
Arguments match output [OK]
Hint: Remember $1, $2 are first and second args; $# is count [OK]
Common Mistakes:
Mixing script name ($0) with arguments
Confusing argument order
Miscounting number of arguments
4. Given this script snippet:
echo "Argument 1: $1"
echo "Argument 2: $2"
What is the error if you run the script without any arguments?
medium
A. Script prints empty lines for $1 and $2
B. No output is printed
C. Script throws a syntax error
D. Script prints the script name instead of arguments
Solution
Step 1: Understand missing arguments behavior
If no arguments are passed, $1 and $2 are empty strings, not errors.
Step 2: Check script output
echo prints "Argument 1: " and "Argument 2: " with nothing after colon.
Final Answer:
Script prints empty lines for $1 and $2 -> Option A
Quick Check:
Missing args = empty $1, $2 [OK]
Hint: No args means $1, $2 are empty, no errors [OK]
Common Mistakes:
Expecting syntax or runtime error
Thinking script name prints as argument
Assuming no output at all
5. You want a bash script to print all arguments passed to it, each on a new line, using only $1, $2, etc. Which approach works best?
hard
A. Use a loop with a counter and access arguments by $1, $2, ... until counter > $#
B. Use shift command in a loop to access each argument and print it
C. Use $0 to print all arguments one by one
D. Use echo $@ to print all arguments on one line
Solution
Step 1: Understand how to access all arguments one by one
Using shift moves arguments left, so $1 becomes next argument each time.
Step 2: Compare options
Use a loop with a counter and access arguments by $1, $2, ... until counter > $# is complex and error-prone; Use echo $@ to print all arguments on one line prints all on one line; Use $0 to print all arguments one by one prints script name; Use shift command in a loop to access each argument and print it correctly loops and prints each argument on new line.
Final Answer:
Use shift command in a loop to access each argument and print it -> Option B
Quick Check:
Shift loops through args one by one [OK]
Hint: Use shift in loop to print each argument separately [OK]
Common Mistakes:
Using $0 instead of arguments
Trying to manually count without shift
Printing all args on one line instead of separate lines
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.