LearnLinux FoundationsBash Shell Environment

Escape Characters

echo - a hands-on Linux lab on a real virtual machine.

The backslash is the finest-grained quoting tool: it tells the shell to take the NEXT single character literally, switching off whatever special meaning it would carry. Every drill runs the escape through echo so the silent parsing change becomes a visible line: escape a dollar sign so it is not a variable, escape a space so it stops splitting words, escape a backslash to print one, and escape a double quote inside double quotes. See the two zones (a backslash is literal inside single quotes, and inside double quotes escapes only $, double-quote, backslash, and backtick) and the line-continuation trick where a trailing backslash escapes the newline. A companion to the quoting lesson.

You want to print a price. You type what looks obvious:

echo "Price: $5"

and instead of Price: $5 you get Price: with the number gone. The shell saw $5 and tried to treat it as a variable, swallowing it before echo ever ran. One character, a single backslash, fixes this completely, and by the end of this lesson you will know exactly why.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. Almost everything here prints the exact same result on any machine, because echo just prints back what the shell hands it. The one spot that can vary is the very first contrast above, and that step says so when you reach it.

An escape character is a single backslash, \, that tells the shell to take the NEXT one character literally. It switches off whatever special meaning that character would normally carry, just for that one character, and then normal rules resume.

Think of the backslash as a tap on the shoulder that says quiet down to the character right after it. A $ normally means start a variable. Put a backslash in front, \$, and that $ becomes a plain dollar sign with no power. This is the finest-grained tool you have: quotes wrap a whole run of text, but the backslash escapes exactly ONE character at a time.

This is the companion to the quoting lesson. Single quotes made EVERYTHING between them literal in one stroke. The backslash is the opposite scale: it makes just one character literal. Reach for quotes when a whole stretch is special; reach for the backslash when it is a single character in otherwise ordinary text.

Back to the price. The problem was that $5 looked like a variable to the shell. Put a backslash directly in front of the dollar sign and its special meaning is switched off, so it prints as a plain dollar sign.

Before you run it, commit to a picture: the backslash itself does not print. It does its job (quiet down that $) and then vanishes, leaving just the dollar sign and the 5.

echo "Price: \$5"

prompt: student@linuxcamp:~$ answer: echo "Price: \$5" output: Price: $5 hint: Put a single backslash right before the dollar sign, inside the quotes: echo "Price: \$5"

The backslash escaped the $, so the shell stopped reading it as the start of a variable and passed a plain $5 straight through to echo. Notice what did NOT appear: the backslash. An escape character is a signal, not a printed symbol. It spends itself protecting the character after it and then it is gone, which is exactly why Price: \$5 prints as Price: $5 and not Price: \$5.

This is the one contrast in the lesson whose UNESCAPED form can look different on different machines. Without the backslash, $5 is read as a positional parameter, a slot that is usually empty in a normal interactive shell, so it prints as nothing. In a script that was handed a fifth argument, that same $5 could print that argument instead. The escaped form, \$5, is rock solid everywhere: it always prints a literal $5.

A space is special too. The shell uses spaces to chop your line into separate words, called arguments. So echo a b hands echo two arguments, a and b, and echo joins them back with a single space when it prints.

Escape the space and it stops being a separator. A backslash in front of a space, \ , makes that space an ordinary character, so a and b arrive as ONE argument with a real space locked inside it.

The printed line looks the same either way, so to prove the difference is real you will make the shell show you the space it kept. Run this:

echo a\ b

prompt: student@linuxcamp:~$ answer: echo a\ b output: a b hint: Backslash immediately before the space, no quotes: echo a\ b

You saw a b, one word with a space held inside it, because the backslash told the shell that space is not a word separator, it is part of the text. The visible result looks identical to an ordinary space, and that is the whole point of an escape: it changes how the shell PARSES the line, not how the result looks. Here the backslash quietly turned a separator into a plain character.

So how do you print a backslash, when the backslash's whole job is to escape the next character? You escape it with another backslash. The first backslash says take the next character literally, and the next character is a backslash, so you get one real backslash printed.

Before you run it, decide what you expect: two backslashes typed, but how many printed?

echo \\

prompt: student@linuxcamp:~$ answer: echo \\ output: \ hint: Two backslashes: the first escapes the second: echo \\

Two backslashes went in and one came out. The first backslash is the escape signal; it protects the second backslash and then spends itself, exactly like it did with the $. What survives is a single literal backslash. This is the tell-tale sign of an escape character: it always disappears in favour of the one character it was guarding.

Here is a knot the backslash unties cleanly. You want to print a sentence that itself contains double quotes: She said "hi". If you just wrap the whole thing in double quotes, the inner quote closes the string early and the shell gets confused about where your text ends.

The fix is to escape each inner double quote with a backslash, \", so the shell reads it as a plain quote character to print, not as the end of the string.

echo "She said \"hi\""

prompt: student@linuxcamp:~$ answer: echo "She said \"hi\"" output: She said "hi" hint: Backslash before each inner double quote: echo "She said \"hi\""

Each \" reached echo as a plain double quote to be printed, not as a string boundary, so the sentence came out with its quotes intact: She said "hi". Inside double quotes the backslash is picky about what it will escape, and a double quote is one of the few characters it acts on. That is the next thing to pin down.

Where you put the backslash changes how much it does. There are two zones, and they behave differently.

Inside single quotes, the backslash is just a backslash. Single quotes already make everything literal, so nothing gets escaped in there. A backslash prints as a plain backslash. Prove it:

echo 'a\b'

prompt: student@linuxcamp:~$ answer: echo 'a\b' output: a\b hint: Single quotes make everything literal, backslash included: echo 'a\b'

Inside single quotes the backslash printed itself: a\b, backslash and all. Single quotes are the strongest, bluntest quoting there is, so no escaping happens inside them and no escaping is NEEDED. That is the trade: single quotes cover a whole run of text at once, but you cannot slip an escape past them. When you are unquoted, or inside double quotes, the backslash is live again.

Inside double quotes, the backslash escapes only four characters: the dollar sign $, the double quote ", the backslash \ itself, and the backtick ` `. Before any other character inside double quotes, a backslash just stays a plain backslash. So \$ and \" do their job in there, but \b` prints as a literal backslash-b. Outside quotes entirely, the backslash escapes whatever single character follows it.

A backslash has one more trick, and it only fires in one exact spot: at the very END of a line. There, the backslash escapes the invisible newline character. That switches off the newline's usual meaning, which is run the command now, so instead the shell treats your next line as a continuation of the same command. This is called line continuation, and it lets you spread one long command across several tidy lines.

Type the two lines below. End the first line in a backslash and press Enter. The shell shows a > continuation prompt asking for more, and the whole thing runs as one command once you finish the second line.

echo one \ two

prompt: student@linuxcamp:~$ answer: echo one \ output: one two hint: End the first line with a lone backslash, then type the rest on the next line: echo one \ then two

The trailing backslash escaped the newline, so the shell did not run anything yet: it printed a > prompt and waited for the rest. When you finished with two, it stitched both lines into the single command echo one two and printed one two. Same escape idea as everywhere else in this lesson, aimed at the one character you cannot see: the newline.

This is also the classic trap. Type a trailing backslash by accident, press Enter, and the shell drops to a > prompt and just sits there, seemingly frozen. It is not frozen, it is waiting for the rest of a command it thinks you started. If you land there by mistake, press Ctrl-C to cancel and get your normal prompt back, then retype the line without the stray backslash on the end.

Scaffolding off. No command is printed this time. You have every piece you need.

Print the exact text Cost: $9 so the dollar sign shows up literally and the shell does not try to read $9 as a variable. Use echo, and switch off the special meaning of that one dollar sign.

prompt: student@linuxcamp:~$ answer: echo "Cost: \$9" output: Cost: $9 hint: Same move as the price example. Put a single backslash in front of the dollar sign so it loses its power.

You escaped the $ with a backslash, so it printed as a plain dollar sign and $9 was never read as a variable. The backslash spent itself and vanished, leaving Cost: $9 exactly as typed. That is the everyday use of an escape character: one backslash, one protected character, and a symbol that would otherwise have special meaning comes out plain. You recalled the move from memory, which is the same recall the real machine will ask of you.

You earned this cheat sheet. Every row is an escape you just ran:

And the rules that tie it together: a backslash makes the NEXT single character literal. Inside single quotes it does nothing (everything is already literal). Inside double quotes it only escapes $, ", \, and ` ``. At the very end of a line it escapes the newline, giving you line continuation.

Two backslashes catch beginners. First, forgetting to escape a $, so a variable expands and eats your text: the fix is always \$. Second, a stray trailing backslash that drops you at a lonely > prompt: that is line continuation waiting for input, and Ctrl-C gets you out. Both are the same idea seen from two sides: the backslash always acts on the very next character, even when that character is an invisible newline.

The practice terminal has shown you the backslash from every angle. You escaped a dollar sign, a space, a backslash itself, and a quote inside quotes, you saw why single quotes need no escaping, and you continued a command across two lines. Every one of those you typed yourself.

The Bash Environment module ends with one real Linux machine, the Bash Environment capstone mission, and that is where you use escaping for real. A VM boots just for you, with its own shell. It hands you objectives that lean on everything this module taught: variables, quoting, expansions, and the backslash that switches a single character back to plain text. One difference from here: the mission shows no commands. You read the objective, you recall the escape, you type it. That recall is what makes it stick.

Finish the other Bash Environment lessons, then go escape a character on a real shell for yourself.

Practice Escape Characters in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.