Single quotes preserve the exact text inside them. No variables or special characters are expanded or interpreted.
Click to reveal answer
beginner
How does bash treat variables inside single quotes? For example: 'Hello $USER'
Bash treats everything inside single quotes literally. So '$USER' is not replaced by the username; it stays as the text $USER.
Click to reveal answer
beginner
Which of these strings will print the username in bash?
A) 'Hello $USER'
B) "Hello $USER"
B) "Hello $USER" will print the username because double quotes allow variable expansion. Single quotes do not.
Click to reveal answer
intermediate
Can you include a single quote inside a single-quoted string directly in bash?
No, you cannot directly include a single quote inside single quotes. You must close the quotes, add an escaped single quote, then reopen them.
Click to reveal answer
intermediate
Example: How to print: It's a nice day using single quotes in bash?
Use: echo 'It'\''s a nice day'
This closes the single quote, adds an escaped single quote, then reopens the single quote.
Click to reveal answer
What will this command output? echo 'Today is $DATE'
AToday is followed by the value of DATE variable
BNothing, blank line
CToday is $DATE
DError because $DATE is not expanded
✗ Incorrect
Single quotes prevent variable expansion, so $DATE is printed literally.
Which quotes allow variable expansion in bash?
ASingle quotes
BDouble quotes
CBackticks
DNo quotes
✗ Incorrect
Double quotes allow variables to expand, single quotes do not.
How do you include a single quote inside a single-quoted string?
AClose single quotes, add escaped single quote, reopen single quotes
BUse double quotes instead
CJust type it inside
DUse backslash inside single quotes
✗ Incorrect
You must close the single quotes, add an escaped single quote, then reopen single quotes.
What does this command print? echo 'Price is $5'
APrice is $5
BPrice is \$5
CPrice is $5 with variable expansion
DError
✗ Incorrect
Inside single quotes, backslash is literal, so $ prints as $.
Why use single quotes in bash scripts?
ATo allow variable expansion
BTo comment out lines
CTo run commands inside
DTo preserve exact text without changes
✗ Incorrect
Single quotes keep the text exactly as typed, no expansions or interpretations.
Explain how single quotes affect variable expansion and special characters in bash strings.
Think about how bash treats text inside single quotes compared to double quotes.
You got /3 concepts.
Describe the method to include a single quote character inside a single-quoted string in bash.
Remember you cannot put a single quote directly inside single quotes.
You got /4 concepts.
Practice
(1/5)
1. What does enclosing text in single quotes 'text' do in a Bash script?
easy
A. Runs commands inside the quotes and replaces with output.
B. Allows variables inside to be expanded to their values.
C. Removes all spaces inside the quotes.
D. Treats the text exactly as written, no variable or command expansion.
Solution
Step 1: Understand single quotes behavior
Single quotes in Bash preserve the text literally without any expansion.
Step 2: Compare with other quotes
Double quotes allow variable and command expansion, but single quotes do not.
Final Answer:
Treats the text exactly as written, no variable or command expansion. -> Option D
Quick Check:
Single quotes = literal text [OK]
Hint: Single quotes keep text exactly as typed, no changes inside [OK]
Common Mistakes:
Thinking variables expand inside single quotes
Assuming commands run inside single quotes
Confusing single quotes with double quotes
2. Which of the following is the correct way to assign the literal string It's sunny to a variable weather using single quotes in Bash?
easy
A. weather='It's sunny'
B. weather='It'\''s sunny'
C. weather="It's sunny"
D. weather='It\'s sunny'
Solution
Step 1: Understand single quote limitation
Single quotes cannot contain a single quote directly inside; it ends the string.
Step 2: Use escape trick for single quote inside single quotes
Use 'It'\''s sunny' which closes, escapes, and reopens single quotes.
Final Answer:
weather='It'\''s sunny' -> Option B
Quick Check:
Single quote inside single quotes needs closing and escaping [OK]
Hint: Close, escape, reopen single quotes to include a single quote inside [OK]
Common Mistakes:
Putting single quote directly inside single quotes
Using backslash inside single quotes (ignored)
Confusing with double quotes for this case
3. What will be the output of this Bash script?
name='Alice'
echo 'Hello, $name!'
medium
A. Hello, Alice!
B. Hello, !
C. Hello, $name!
D. Syntax error
Solution
Step 1: Identify single quotes effect on variables
Variables inside single quotes are not expanded; they print literally.
Step 2: Check the echo output
Echo prints exactly 'Hello, $name!' as typed inside single quotes.
Final Answer:
Hello, $name! -> Option C
Quick Check:
Single quotes prevent variable expansion [OK]
Hint: Variables inside single quotes print as-is, no expansion [OK]
Common Mistakes:
Expecting variable to expand inside single quotes
Confusing single and double quotes
Assuming syntax error due to variable
4. The following script is intended to print Path: /usr/bin but it prints Path: $PATH instead. What is the error?
echo 'Path: $PATH'
medium
A. Single quotes prevent variable expansion, use double quotes instead.
B. Variable name is incorrect, should be lowercase path.
C. Missing backslash before $ to escape variable.
D. echo command syntax is wrong.
Solution
Step 1: Understand single quotes effect on variables
Single quotes treat $PATH literally, no expansion happens.
Step 2: Fix by using double quotes
Double quotes allow variable expansion, so use "Path: $PATH".
Final Answer:
Single quotes prevent variable expansion, use double quotes instead. -> Option A
Quick Check:
Single quotes block expansion, double quotes allow it [OK]
Hint: Use double quotes to expand variables, not single quotes [OK]
Common Mistakes:
Thinking variable name case matters here
Trying to escape $ inside single quotes (ignored)
Assuming echo syntax error
5. You want to create a Bash script that prints the exact text She said, 'Hello!' using a single echo command with single quotes. Which command will work correctly?
hard
A. echo 'She said, '\''Hello!\'''
B. echo 'She said, "Hello!"'
C. echo "She said, 'Hello!'"
D. echo 'She said, 'Hello!''
Solution
Step 1: Recognize single quote inside single quotes problem
Single quotes cannot contain single quotes directly; it ends the string.
Step 2: Use the escape trick to include single quote inside single quotes
Use 'She said, '\''Hello!\''' to close, escape, and reopen single quotes.
Final Answer:
echo 'She said, '\''Hello!\''' -> Option A
Quick Check:
Escape single quote inside single quotes by closing and reopening [OK]
Hint: Close, escape, reopen single quotes to include single quote inside [OK]
Common Mistakes:
Putting single quote directly inside single quotes
Using double quotes when single quotes required
Incorrectly escaping quotes inside single quotes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.