0
0
Bash Scriptingscripting~20 mins

Double quotes (variable expansion) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Double Quotes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script?
Consider the following Bash script:

name=World

echo "Hello $name!"
echo 'Hello $name!'

What will be printed when this script runs?
Bash Scripting
name=World

echo "Hello $name!"
echo 'Hello $name!'
AHello World!\nHello $name!
BHello $name!\nHello $name!
CHello World!\nHello World!
DHello $name!\nHello World!
Attempts:
2 left
💡 Hint
Remember that double quotes allow variable expansion, but single quotes do not.
💻 Command Output
intermediate
2:00remaining
What does this script print?
Given this Bash script:

var=5

echo "$var is a number"
echo '$var is a number'

What is the output?
Bash Scripting
var=5

echo "$var is a number"
echo '$var is a number'
A$var is a number\n$var is a number
B$var is a number\n5 is a number
C5 is a number\n$var is a number
D5 is a number\n5 is a number
Attempts:
2 left
💡 Hint
Check how variables behave inside double vs single quotes.
🔧 Debug
advanced
2:00remaining
Why does this script not print the variable value?
Look at this Bash script:

greeting=Hello

echo '$greeting, friend!'

Why does it print '$greeting, friend!' instead of 'Hello, friend!'?
Bash Scripting
greeting=Hello

echo '$greeting, friend!'
ABecause the variable greeting is not defined properly.
BBecause single quotes prevent variable expansion, so $greeting is printed literally.
CBecause echo does not support variables inside quotes.
DBecause the variable name is misspelled.
Attempts:
2 left
💡 Hint
Try changing single quotes to double quotes.
💻 Command Output
advanced
2:00remaining
What is the output of this script with mixed quotes?
Consider this Bash script:

user=Alice

echo "User is '$user'"
echo 'User is "$user"'

What will be printed?
Bash Scripting
user=Alice

echo "User is '$user'"
echo 'User is "$user"'
AUser is 'Alice'\nUser is "$user"
BUser is '$user'\nUser is "Alice"
CUser is 'Alice'\nUser is "Alice"
DUser is $user\nUser is $user
Attempts:
2 left
💡 Hint
Remember how quotes inside quotes affect variable expansion.
🚀 Application
expert
2:00remaining
How many items are in the array after this script runs?
Given this Bash script:

items="apple banana cherry"

arr=($items)

echo ${#arr[@]}

What number will be printed?
Bash Scripting
items="apple banana cherry"

arr=($items)

echo ${#arr[@]}
A1
B0
CSyntaxError
D3
Attempts:
2 left
💡 Hint
Check how the string is split into array elements.