Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Double quotes (variable expansion) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Double quotes (variable expansion)
Start script
↓
Assign variable
↓
Use variable inside double quotes
↓
Shell expands variable
↓
Print expanded value
↓
End script
The shell reads the script, assigns variables, then expands variables inside double quotes before printing the result.
Execution Sample
Bash Scripting
name="Alice"
echo "Hello, $name!"
Assigns 'Alice' to variable name and prints 'Hello, Alice!' using double quotes for variable expansion.
Execution Table
StepActionVariable 'name'CommandOutput
1Assign variableAlicename="Alice"
2Expand variable inside double quotesAliceecho "Hello, $name!"Hello, Alice!
3End scriptAlice
💡 Script ends after printing the expanded variable inside double quotes.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefinedAliceAliceAlice
Key Moments - 2 Insights
Why does the variable $name get replaced with 'Alice' inside double quotes?
Because inside double quotes, the shell expands variables, so $name is replaced by its value 'Alice' as shown in step 2 of the execution_table.
What would happen if single quotes were used instead of double quotes?
Single quotes prevent variable expansion, so the output would literally be 'Hello, $name!' instead of 'Hello, Alice!'. This is different from the double quotes behavior in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
AHello, $name!
BHello, !
CHello, Alice!
Dname
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step is the variable 'name' assigned the value 'Alice'?
AStep 1
BStep 2
CStep 3
DBefore the script starts
💡 Hint
Look at the 'Action' and 'Variable' columns in the execution_table for when 'name' changes.
If we replaced double quotes with single quotes in echo, what would be the output?
AHello, Alice!
BHello, $name!
CHello, !
DError
💡 Hint
Recall that single quotes prevent variable expansion, unlike double quotes shown in step 2.
Concept Snapshot
Double quotes allow variable expansion in bash.
Use "$variable" to get its value inside strings.
Single quotes prevent expansion, printing variables literally.
Example: name="Alice"; echo "Hello, $name!" prints Hello, Alice!
Always use double quotes to safely expand variables in strings.
Full Transcript
This example shows how bash expands variables inside double quotes. First, the variable 'name' is assigned the value 'Alice'. Then, when echo is called with "Hello, $name!", the shell replaces $name with 'Alice' because double quotes allow variable expansion. The output is 'Hello, Alice!'. If single quotes were used instead, the output would be 'Hello, $name!' because single quotes prevent expansion. This behavior is important to understand for scripting and printing variables correctly.

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

  1. Step 1: Understand variable expansion in double quotes

    In bash, variables inside double quotes are replaced by their values.
  2. Step 2: Compare with other quote types

    Single quotes prevent expansion, but double quotes allow it while preserving spaces.
  3. Final Answer:

    The variable name is replaced by its value inside the quotes. -> Option A
  4. 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

  1. Step 1: Identify correct quoting for variable expansion

    Double quotes allow variable $user to expand inside the string.
  2. 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.
  3. Final Answer:

    echo "Hello, $user!" -> Option D
  4. Quick Check:

    Use double quotes to expand variables in strings = B [OK]
Hint: Use double quotes to expand variables inside strings [OK]
Common Mistakes:
  • Using single quotes which prevent expansion
  • Not quoting strings with spaces
  • Mixing single and double quotes incorrectly
3. Given the script:
name="Alice"
echo "Hello, $name!"
echo 'Hello, $name!'

What is the output?
medium
A. Hello, Alice! Hello, Alice!
B. Hello, Alice! Hello, $name!
C. Hello, $name! Hello, Alice!
D. Hello, $name! Hello, $name!

Solution

  1. Step 1: Analyze first echo with double quotes

    Double quotes expand $name to "Alice", so output is "Hello, Alice!".
  2. Step 2: Analyze second echo with single quotes

    Single quotes prevent expansion, so output is literally "Hello, $name!".
  3. Final Answer:

    Hello, Alice! Hello, $name! -> Option B
  4. 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

  1. Step 1: Identify issue with first echo

    Without quotes, echo $file splits "my file.txt" into two words, printing as two separate arguments.
  2. Step 2: Fix by adding double quotes

    Using echo "$file" preserves the space and prints the full filename correctly.
  3. Final Answer:

    First echo should use double quotes to preserve spaces: echo "$file". -> Option A
  4. 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

  1. Step 1: Check condition with quotes

    Using [ "$count" ] safely tests if count is non-empty, even if it contains spaces or is unset.
  2. Step 2: Print message with double quotes

    Using double quotes in echo expands $count properly inside the string.
  3. Final Answer:

    if [ "$count" ]; then echo "Count is $count"; fi -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard