A backslash removes the special meaning of one character
Outside of single quotes, a backslash makes the next character literal. A backslash before a space joins two words into one, a backslash before a dollar sign stops parameter expansion, and a backslash before an asterisk stops filename expansion. A backslash before another backslash produces one literal backslash. The rule is simple, and it applies at the level of a single character, which makes it useful for protecting exactly one special symbol while leaving the rest of a word alone.
echo \$HOME
echo a\ b
echo \*
echo \\$HOME
a b
*
\The second line printed a single argument, a b, even though the command contains what looks like two words. The backslash joined them. This is an alternative to quoting, and it is useful when you are typing at the prompt, because a single backslash is quicker than a pair of quote marks.
Line continuation
When a backslash is the last character on a line, bash removes both the backslash and the newline that follows it. The two lines then act as one. This is how long commands are split across several lines in scripts and documentation. The removal is exact: no space is inserted, so you must put the space yourself if the words need one.
echo alpha\
betaalphabetaThe output has no space between the two words, because none was typed. A common mistake is to put a space after the backslash. That space is a real character, and the backslash then protects it, so the next line is not a continuation at all. Check that the backslash is the very last character on the line.
Escapes belong to the program that reads them
A backslash sequence such as \t has no meaning to bash when it sits in an ordinary word. Whether it becomes a tab depends on the program. The printf builtin processes backslash escapes in its format string, so printf 'a\tb\n' prints a real tab. The echo builtin in bash does not process them by default, so echo 'a\tb' prints the four characters. When you see an escape that does not work, the first question is which program is supposed to interpret it. Bash handles escapes only in the quoting forms covered above and in the escapes of the here-document, and everything else is passed to the command as written.
Escaping inside a here-document
A here-document feeds several lines of text to a command. If the delimiter is unquoted, the text undergoes expansion, and the backslash keeps its special meaning for the dollar sign, the backtick, another backslash, and the newline. That lets you insert a variable and a literal dollar sign on the same line. The example writes the value of HOME and then the literal text $HOME.
cat <<EOF
home is $HOME
literal is \$HOME
EOFhome is /workspace
literal is $HOMEIf the delimiter is quoted, as in <<'EOF', nothing in the text is expanded and every backslash is kept. Choose the quoted form for a block of text that contains many dollar signs, such as a script you are writing to a file.
Backslashes inside double quotes
Double quotes change the rules. Inside them, a backslash keeps its special meaning only before a dollar sign, a backtick, a double quote, another backslash, or a newline. Before any other character it is kept as written. The result is that a backslash is safe to use in a double-quoted path, as long as the next character is not one of those five.
echo "cost: \$5"
echo "path: C:\temp"
echo "a \"quoted\" word"cost: $5
path: C:\temp
a "quoted" wordThe middle line keeps its backslash because t is not a special character inside double quotes. That is exactly the behavior you want for a Windows-style path copied into a script, and it is exactly the surprise that makes people reach for single quotes instead.
Putting a single quote inside single quotes
Single quotes cannot contain a single quote, so the usual trick is to close the quoted string, add an escaped quote, and open a new string. The sequence '\'' does this. It ends the first string, writes a literal quote with a backslash, and starts the next string. Adjacent pieces form one word, so the whole thing is one argument.
echo 'it'\''s fine'it's fineThe pattern looks cryptic, but it is common in scripts that generate quoted strings. The ANSI-C form $'it\'s' is shorter where it is available. Choose whichever is easier for the next reader.
Let printf do the quoting
When you need to generate text that another shell will read back, do not write the escapes by hand. The %q format of printf prints a version of its argument with backslashes in front of the characters that need them. It is the safest way to write a value into a file that a script will later source or execute. A simple case is a value with a space, which prints with the space escaped, so the shell reads it back as one word.
printf '%q\n' 'a b'a\ bA practical rule
Use single quotes for fixed text, double quotes for values, and the backslash for a single special character in an otherwise plain word. If you find yourself writing many backslashes in a row, the command probably wants a different quoting style or a variable. The best escaping is the kind a reader does not have to decode. The lab asks you to use each of the three tools once: a backslash for a dollar sign, the quote-closing trick for an apostrophe, and a line continuation to join two lines.