0
0
Bash Scriptingscripting~5 mins

Escape characters (\) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the escape character (\) in bash scripting?
The escape character (\) tells the shell to treat the next character literally, ignoring its special meaning. It helps include special characters in strings or commands.
Click to reveal answer
beginner
How do you include a literal dollar sign ($) in a bash string?
Use the escape character before the dollar sign like this: \$. This prevents the shell from interpreting it as a variable.
Click to reveal answer
intermediate
What does this command print? <br>echo "Hello\nWorld"
It prints: Hello\nWorld (with the \n shown literally) because double quotes allow escape characters but echo does not interpret \n as a newline by default.
Click to reveal answer
intermediate
How do you write a newline character inside a bash string using escape sequences?
Use \n inside a string with the -e option in echo, like: echo -e "Line1\nLine2" to print on two lines.
Click to reveal answer
beginner
Why do you need to escape a backslash (\\) itself in bash?
Because the backslash is the escape character, to print a literal backslash you must escape it with another backslash: \\.
Click to reveal answer
What does the escape character (\) do in bash?
AMakes the next character literal
BDeletes the next character
CRepeats the next character twice
DComments out the line
How do you print a literal double quote inside a double-quoted string in bash?
A\'
B""
C'"'
D\"
Which command prints two lines using escape sequences?
Aecho -n "Hello\nWorld"
Becho "Hello\nWorld"
Cecho -e "Hello\nWorld"
Decho Hello\nWorld
How do you write a literal backslash in bash?
A\\
B\
C/
D//
What happens if you do not escape a special character in bash?
AIt is ignored
BIt is interpreted with its special meaning
CIt causes an error always
DIt is printed literally
Explain how the escape character (\) works in bash scripting and give two examples of its use.
Think about how to include special characters in strings.
You got /3 concepts.
    Describe how to print a newline in bash using echo and escape sequences.
    Remember echo needs a special option to interpret \n.
    You got /3 concepts.