📖 Scenario: Imagine you are organizing a small event and need to keep track of the number of guests invited. Instead of typing the number every time, you want to store it once and reuse it easily in your script.
🎯 Goal: You will create a bash script that stores the number of guests in a variable, updates it, and then prints the final number. This will show how variables help store and reuse data in scripts.
📋 What You'll Learn
Create a variable called guest_count with the initial value 10
Create a variable called new_guests with the value 5
Add the value of new_guests to guest_count and store the result back in guest_count
Print the final value of guest_count using echo
💡 Why This Matters
🌍 Real World
Event planners and organizers often need to keep track of numbers like guests, seats, or items. Using variables in scripts helps automate these tasks.
💼 Career
Knowing how to store and reuse data with variables is a basic skill for automation, system administration, and scripting jobs.
Progress0 / 4 steps
1
Create the initial variable
Create a variable called guest_count and set it to 10.
Bash Scripting
Hint
Use variable_name=value format without spaces around the equals sign.
2
Add a new variable for new guests
Create a variable called new_guests and set it to 5.
Bash Scripting
Hint
Remember to use the same format as before for variables.
3
Update the guest count
Add the value of new_guests to guest_count and store the result back in guest_count using arithmetic expansion.
Bash Scripting
Hint
Use $(( )) to do arithmetic in bash.
4
Print the final guest count
Print the value of guest_count using echo.
Bash Scripting
Hint
Use echo $guest_count to display the value.
Practice
(1/5)
1. What is the main reason to use variables in a bash script?
easy
A. To create new files automatically
B. To make the script run faster
C. To store data so it can be reused later
D. To change the script's permissions
Solution
Step 1: Understand the purpose of variables
Variables hold data temporarily so you can use it multiple times without rewriting it.
Step 2: Identify the correct purpose
Storing data for reuse matches the main reason to use variables in bash scripting.
Final Answer:
To store data so it can be reused later -> Option C
Quick Check:
Variables store and reuse data = C [OK]
Hint: Variables save data for reuse, like a labeled box [OK]
Common Mistakes:
Thinking variables speed up the script
Confusing variables with file operations
Assuming variables change permissions
2. Which is the correct way to assign the value "hello" to a variable named greeting in bash?
easy
A. greeting == "hello"
B. greeting = "hello"
C. greeting: "hello"
D. greeting="hello"
Solution
Step 1: Recall bash variable assignment syntax
In bash, no spaces are allowed around the equals sign when assigning variables.
Step 2: Identify the correct syntax
greeting="hello" uses no spaces and quotes the string correctly, matching bash rules.
Final Answer:
greeting="hello" -> Option D
Quick Check:
No spaces around = in bash assignment = A [OK]
Hint: No spaces around = when assigning variables in bash [OK]