📖 Scenario: You are managing a small store's daily sales. You want to calculate the total number of items sold by adding the sales from morning and afternoon shifts.
🎯 Goal: Build a simple Bash script that uses arithmetic expansion $(( )) to add two numbers representing sales and display the total.
📋 What You'll Learn
Create two variables morning_sales and afternoon_sales with exact values
Create a variable total_sales that uses arithmetic expansion $(( )) to add the two sales variables
Print the total sales using echo
💡 Why This Matters
🌍 Real World
Calculating totals, counts, or simple math in shell scripts is common for automating tasks like sales reports or inventory checks.
💼 Career
Knowing arithmetic expansion helps you write scripts that handle numbers easily, a useful skill for system administrators and automation engineers.
Progress0 / 4 steps
1
Set up sales variables
Create two variables called morning_sales and afternoon_sales with values 120 and 80 respectively.
Bash Scripting
Hint
Use variable=value format without spaces around =.
2
Calculate total sales
Create a variable called total_sales that uses arithmetic expansion $(( )) to add morning_sales and afternoon_sales.
Bash Scripting
Hint
Use total_sales=$((morning_sales + afternoon_sales)) to add the two variables.
3
Display the total sales
Use echo to print the value of total_sales.
Bash Scripting
Hint
Use echo $total_sales to show the result.
4
Run the script and see the result
Run the script to display the total sales number. The output should be 200.
Bash Scripting
Hint
Run the script in your terminal using bash script.sh or ./script.sh if executable.
Practice
(1/5)
1. What does the expression $((3 + 4)) do in a bash script?
easy
A. Concatenates 3 and 4 as strings to get 34
B. Calculates the sum of 3 and 4 and returns 7
C. Throws a syntax error because of missing quotes
D. Assigns 3 to variable 4
Solution
Step 1: Understand arithmetic expansion syntax
The $(( )) syntax evaluates the expression inside as an arithmetic operation.
Step 2: Calculate the expression inside
Inside the parentheses, 3 + 4 equals 7.
Final Answer:
Calculates the sum of 3 and 4 and returns 7 -> Option B
Quick Check:
3 + 4 = 7 [OK]
Hint: Remember $(( )) does math, not string join [OK]
Common Mistakes:
Thinking it concatenates numbers as strings
Forgetting to use $(( )) for arithmetic
Expecting quotes around the expression
2. Which of the following is the correct syntax to add two variables a and b using arithmetic expansion in bash?
Division by zero is not allowed and causes a runtime error in bash arithmetic expansion.
Final Answer:
Division by zero error causes runtime failure -> Option D
Quick Check:
Division by zero is invalid in $(( )) [OK]
Hint: Never divide by zero inside $(( )) [OK]
Common Mistakes:
Thinking it's a syntax error
Assuming division by zero returns zero
Believing arithmetic expansion disallows division
5. You want to calculate the factorial of a number stored in variable n using a bash loop and arithmetic expansion. Which snippet correctly updates the factorial value inside the loop?
hard
A. fact=$[fact * i]
B. fact=$(fact * i)
C. fact=$((fact * i))
D. fact=$((fact + i))
Solution
Step 1: Understand factorial calculation logic
Factorial multiplies all integers from 1 to n, so fact must be multiplied by i each loop.
Step 2: Identify correct arithmetic expansion syntax for multiplication
fact=$((fact * i)) uses $((fact * i)), which correctly multiplies fact by i and updates fact.
Final Answer:
fact=$((fact * i)) -> Option C
Quick Check:
Use $(( )) for multiplication updates in loops [OK]
Hint: Use $((fact * i)) to multiply and update variable [OK]
Common Mistakes:
Using $() which runs commands, not math
Using $[] which is deprecated
Adding instead of multiplying for factorial
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.