ArticleslgStudy

science

Escape sequence

Escape sequence is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Escape sequence rather than just read about it. In short: In computing, an escape sequence is a sequence of characters that has a special semantic meaning based on an established convention that specifies an escape character prefix in addition to the syntax of the rest of the text of a sequence. A convention can define any particular character code as a sequence prefix.

Key takeaways

  • Escape sequence belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Escape sequence to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Escape sequence from memory before moving on to harder problems.

Reference excerpt

In computing, an escape sequence is a sequence of characters that has a special semantic meaning based on an established convention that specifies an escape character prefix in addition to the syntax of the rest of the text of a sequence. A convention can define any particular character code as a sequence prefix. Some conventions use a normal, printable character such as backslash (\) or ampersand (&). Others use a non-printable (a.k.a. control) character such as ASCII escape. Escape sequences date back at least to the 1874 Baudot code.

Examples

Data transmission A common use of an escape sequence is to remove control characters from a data stream so that it does not cause its control function by mistake. The control character is replaced with an escape character and one or more other subsequent characters. After escaping the normal context in which the control character would have caused an action, the sequence is replaced by the removed character. To transmit the escape character itself, two copies are sent.

Text literal An escape sequence is often used in character and string literals, to express characters that are not printable or clash with the syntax of characters or strings. For example, control characters might not be allowed in a source file or may have undesirable side-effects if typed into a command. A feature of C and many programming languages influenced by it is that the beginning of an escape sequence in such text literals is marked by a backslash (\). Common examples of these C-style escape sequences include \r, \n and \t for the carriage return, newline, and tab characters, respectively. To account for the fact that using a printable character for escape causes that character to lose its normal meaning, a sequence of two backslash characters (\\) is provided to express a single backslash. There is also an escape sequence that expresses any character by its ASCII-code value. For example, the backslash can be expressed as either \x5c or \134, which specify the character code value in hexadecimal and octal notations, respectively. Likewise, the lowercase e can be expressed as \145 and the digit 7 as \067. A backslash immediately followed by a newline (which is necessarily outside of a string literal) does not mark an escape sequence. Instead, it can be thought of as meaning "Disregard the newline": the C preprocessor joins the line with the subsequent line.

Quoting escape

When an escape character is needed within a string literal, there are two common strategies:

Doubled delimiter – For example, 'He didn''t do it.' Secondary escape sequence – For example, the command prompt command echo Cut^&Paste outputs "Cut&Paste" in by escaping the ampersand operator with a caret (^) In C and many related languages, the escape character is the backslash (\). The single quotation mark character can be coded as '\'' since ''' is not valid. As a string literal is delimited by double-quotes (") the content cannot contain a double-quote unless it is escaped ("\"") or via a sequence that specifies the code of the double-quote character (\x22). In Perl or Python 2, the following is invalid syntax:

This can be fixed by inserted backslash to escape:

Alternatively, the following uses "\x" to indicate the subsequent two characters are hexadecimal digits; "22" being the hexadecimal ASCII value for double-quote.

C, C++, Java, and Ruby allow the same two backslash escape styles. PostScript and rich text format (RTF) also use backslash escapes. The quoted-printable encoding uses the equals sign as an escape character. URL and URI use percent-encoding to quote characters with a special meaning, as for non-ASCII characters.

ANSI escape sequences The VT52 terminal used simple digraph commands like escape-A. Without the escape character prefix, A simply meant the letter A, but as part of the escape sequence escape-A, it had a different meaning. The VT52 also supported parameters. It was not a straightforward control language encoded as substitution. The later VT100 terminal implemented the more sophisticated ANSI escape sequences standard (now ECMA-48) for functions such as controlling cursor movement, character set, and display enhancements. The HP 2640 series had perhaps the most elaborate escape sequences for block and character modes, programming keys and their soft labels, graphics vectors, and even saving data to tape or disk files. In Windows (and MS-DOS), a utility, ANSI.SYS, can be used to enable ANSI escape sequence support. In DOS via $e in the PROMPT command), and in 16-bit Windows via a command window. In Unix and Unix-like systems, the ANSI escape sequences are generally supported by the shell. The rise of GUI applications has reduced the use of escape sequences, yet the ability to provide full-screen, text-based applications is still available.

Related

Control sequence A control sequence is a sequence of characters that changes the state of a computer peripheral instead of conveying the normal information that the characters represent. In an ANSI escape sequence, the escape sequence prefix, called control sequence introducer, can be either ASCII ESC (decimal 27) followed by [ or CSI (decimal 155). Notable systems that did not use an escape character for control sequences include:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Escape sequence

Start with the simplest possible case. Write down what Escape sequence claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Escape sequence before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Escape sequence ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Escape sequence

In research
Escape sequence appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Escape sequence in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Escape sequence is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control characters, so understanding it makes those chapters shorter.
In everyday life
Look for Escape sequence outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Escape sequence” →

How to study Escape sequence in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Escape sequence means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Escape sequence out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Escape sequence in simple terms?

In computing, an escape sequence is a sequence of characters that has a special semantic meaning based on an established convention that specifies an escape character prefix in addition to the syntax of the rest of the text of a sequence. A convention can define any particular character code as a s…

Why does Escape sequence matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Escape sequence?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Escape sequence.

Tags

  • Control characters

Keep exploring