0
0
Bash Scriptingscripting~5 mins

Single quotes (literal strings) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What do single quotes do in bash scripting?
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
Which quotes allow variable expansion in bash?
ASingle quotes
BDouble quotes
CBackticks
DNo quotes
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
What does this command print? echo 'Price is $5'
APrice is $5
BPrice is \$5
CPrice is $5 with variable expansion
DError
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
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.