📖 Scenario: You are writing a simple bash script to store and display your favorite fruit.
🎯 Goal: Create a variable to hold the name of your favorite fruit and then print it.
📋 What You'll Learn
Create a variable called fruit with the value Apple using no spaces around the = sign.
Create a variable called count with the value 3 using no spaces around the = sign.
Use a variable called total to store the sum of count and 2.
Print the value of fruit and total.
💡 Why This Matters
🌍 Real World
Bash scripts often use variables to store data like file names, counts, or user input. Correct variable assignment is essential for scripts to work.
💼 Career
Many IT and DevOps jobs require writing bash scripts for automation. Knowing how to assign and use variables correctly is a basic but crucial skill.
Progress0 / 4 steps
1
Create the fruit variable
Create a variable called fruit and assign it the value Apple with no spaces around the = sign.
Bash Scripting
Hint
Remember, in bash, do not put spaces around the = sign when assigning variables.
2
Create the count variable
Create a variable called count and assign it the value 3 with no spaces around the = sign.
Bash Scripting
Hint
Use the same style as the fruit variable assignment.
3
Calculate the total variable
Create a variable called total that stores the sum of count and 2. Use $(( )) for arithmetic and no spaces around the = sign.
Bash Scripting
Hint
Use total=$((count + 2)) to do arithmetic in bash.
4
Print the variables
Print the value of fruit and total using echo. Use $fruit and $total to access the variables.
Bash Scripting
Hint
Use echo $fruit and echo $total to print the values.
Practice
(1/5)
1. Which of the following is the correct way to assign the value hello to a variable named greet in bash?
easy
A. greet =hello
B. greet = hello
C. greet= hello
D. greet=hello
Solution
Step 1: Understand bash variable assignment rules
Bash does not allow spaces around the = sign when assigning variables.
Step 2: Check each option for spaces
Only greet=hello has no spaces around =. Options B, C, and D have spaces, which cause errors.
Final Answer:
greet=hello -> Option D
Quick Check:
No spaces around = means correct assignment [OK]
Hint: No spaces around = in bash variable assignment [OK]
Common Mistakes:
Adding spaces around = causes syntax errors
Using quotes unnecessarily in simple assignments
Trying to assign with spaces after =
2. Which of the following bash commands correctly assigns the value 42 to the variable num?
easy
A. num=42
B. num= 42
C. num =42
D. num = 42
Solution
Step 1: Identify spaces around the equal sign
Bash requires no spaces around the = sign for variable assignment.
Step 2: Verify each option
Only num=42 has no spaces around =. Others have spaces causing errors.
Final Answer:
num=42 -> Option A
Quick Check:
No spaces around = means correct syntax [OK]
Hint: Remember: no spaces around = in bash assignments [OK]
Common Mistakes:
Leaving spaces around = causes syntax errors
Confusing assignment with comparison operators
Using quotes unnecessarily for numbers
3. What will be the output of the following bash script?
name=John
echo $name
medium
A. name
B. Syntax error
C. John
D. $name
Solution
Step 1: Understand variable assignment and usage
The variable name is assigned the value John without spaces around =.
Step 2: Understand echo with variable
echo $name prints the value stored in name, which is John.
Final Answer:
John -> Option C
Quick Check:
Variable value printed, not variable name [OK]
Hint: Use $ before variable to print its value [OK]
Common Mistakes:
Printing variable name instead of value
Forgetting $ before variable in echo
Adding spaces around = causing errors
4. Identify the error in the following bash script:
count =10
echo $count
medium
A. Spaces around = cause syntax error
B. Missing $ in echo
C. Variable name is invalid
D. No error, script runs fine
Solution
Step 1: Check variable assignment syntax
The assignment count =10 has a space before =, which is not allowed in bash.
Step 2: Understand effect of spaces
Spaces around = cause bash to treat count as a command, leading to syntax error.
Final Answer:
Spaces around = cause syntax error -> Option A
Quick Check:
No spaces around = means valid assignment [OK]
Hint: No spaces around = in assignment to avoid errors [OK]
Common Mistakes:
Adding spaces around = in assignment
Forgetting $ when printing variable
Assuming variable names can have spaces
5. You want to store the current year in a variable year using the command date +%Y. Which of the following is the correct way to assign this output to year in bash?
hard
A. year = $(date +%Y)
B. year=$(date +%Y)
C. year= $(date +%Y)
D. year= '$(date +%Y)'
Solution
Step 1: Understand command substitution syntax
To assign command output, use $(command) without spaces around =.
Step 2: Check each option for spaces and quotes
year=$(date +%Y) has no spaces around = and uses correct command substitution syntax. Options A and C have spaces causing errors. year= '$(date +%Y)' uses quotes which are unnecessary and may cause literal assignment.
Final Answer:
year=$(date +%Y) -> Option B
Quick Check:
No spaces and correct command substitution [OK]
Hint: Use $(command) with no spaces around = for command output assignment [OK]
Common Mistakes:
Adding spaces around = causes errors
Using quotes around command substitution
Forgetting to use $( ) for command output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.