📖 Scenario: You are writing a simple bash script to greet users by their names. You want to learn how to use double quotes so that variables inside strings are expanded correctly.
🎯 Goal: Create a bash script that stores a user's name in a variable and then prints a greeting message using double quotes to expand the variable.
📋 What You'll Learn
Create a variable called name with the value "Alice".
Create a variable called greeting that uses double quotes to include the name variable inside the string.
Print the greeting variable to display the message.
💡 Why This Matters
🌍 Real World
Scripts often need to include user input or dynamic data in messages. Using double quotes correctly helps show the right information.
💼 Career
Understanding variable expansion is essential for writing effective bash scripts used in automation, system administration, and DevOps tasks.
Progress0 / 4 steps
1
Create the name variable
Create a variable called name and set it to the string "Alice".
Bash Scripting
Hint
Use name="Alice" to assign the value with double quotes.
2
Create the greeting variable with double quotes
Create a variable called greeting that stores the string Hello, $name! using double quotes so the name variable expands.
Bash Scripting
Hint
Use double quotes around the string so $name is replaced by its value.
3
Print the greeting variable
Use the echo command to print the value of the greeting variable.
Bash Scripting
Hint
Use echo "$greeting" to print the greeting with variable expansion.
4
Run the script and see the output
Run the script so it prints Hello, Alice! exactly.
Bash Scripting
Hint
Run the script in your terminal and check that it prints Hello, Alice!.
Practice
(1/5)
1. What happens when you use double quotes around a variable in bash, like "$name"?
easy
A. The variable name is replaced by its value inside the quotes.
B. The variable name is treated as plain text, not replaced.
C. The shell throws a syntax error.
D. The variable is removed from the string.
Solution
Step 1: Understand variable expansion in double quotes
In bash, variables inside double quotes are replaced by their values.
Step 2: Compare with other quote types
Single quotes prevent expansion, but double quotes allow it while preserving spaces.
Final Answer:
The variable name is replaced by its value inside the quotes. -> Option A
Quick Check:
Double quotes expand variables = A [OK]
Hint: Double quotes expand variables, single quotes do not [OK]
Common Mistakes:
Thinking variables inside double quotes are not expanded
Confusing single and double quotes
Expecting syntax error with double quotes
2. Which of the following is the correct way to print the value of variable user with a greeting using double quotes in bash?
easy
A. echo 'Hello, $user!'
B. echo "Hello, 'user'!"
C. echo Hello, $user!
D. echo "Hello, $user!"
Solution
Step 1: Identify correct quoting for variable expansion
Double quotes allow variable $user to expand inside the string.
Step 2: Check each option
echo 'Hello, $user!' uses single quotes, so $user is literal. echo Hello, $user! lacks quotes, which can cause word splitting. echo "Hello, 'user'!" quotes 'user' literally inside double quotes.
Final Answer:
echo "Hello, $user!" -> Option D
Quick Check:
Use double quotes to expand variables in strings = B [OK]
Hint: Use double quotes to expand variables inside strings [OK]
Double quotes expand $name to "Alice", so output is "Hello, Alice!".
Step 2: Analyze second echo with single quotes
Single quotes prevent expansion, so output is literally "Hello, $name!".
Final Answer:
Hello, Alice!
Hello, $name! -> Option B
Quick Check:
Double quotes expand, single quotes do not = A [OK]
Hint: Double quotes expand variables; single quotes print literally [OK]
Common Mistakes:
Assuming single quotes expand variables
Confusing output order
Ignoring newline between echo commands
4. Consider this script snippet:
file="my file.txt"
echo $file
ls "$file"
What is the problem and how to fix it?
medium
A. First echo should use double quotes to preserve spaces: echo "$file".
B. Second line should remove quotes: ls $file.
C. No problem; script works fine.
D. Variable should be declared without quotes: file=my file.txt.
Solution
Step 1: Identify issue with first echo
Without quotes, echo $file splits "my file.txt" into two words, printing as two separate arguments.
Step 2: Fix by adding double quotes
Using echo "$file" preserves the space and prints the full filename correctly.
Final Answer:
First echo should use double quotes to preserve spaces: echo "$file". -> Option A
Quick Check:
Quote variables to keep spaces intact = C [OK]
Hint: Quote variables to keep spaces safe in commands [OK]
Common Mistakes:
Not quoting variables with spaces
Removing quotes from commands that need them
Declaring variables without quotes causing errors
5. You want to create a script that prints a message with a variable count inside double quotes, but only if count is not empty. Which is the best way to write this?
hard
A. if [ $count ]; then echo "Count is $count"; fi
B. if [ $count ]; then echo 'Count is $count'; fi
C. if [ "$count" ]; then echo "Count is $count"; fi
D. if [ "$count" ]; then echo 'Count is $count'; fi
Solution
Step 1: Check condition with quotes
Using [ "$count" ] safely tests if count is non-empty, even if it contains spaces or is unset.
Step 2: Print message with double quotes
Using double quotes in echo expands $count properly inside the string.
Final Answer:
if [ "$count" ]; then echo "Count is $count"; fi -> Option C
Quick Check:
Quote variables in tests and output for safety = D [OK]
Hint: Quote variables in tests and output to avoid errors [OK]
Common Mistakes:
Not quoting variable in test causing errors
Using single quotes in echo preventing expansion
Testing variable without quotes causing syntax errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.