Escape character
This article needs additional citations for verification. (April 2010) |
In computing and telecommunications, an escape character is a character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharacters. Generally, the judgement of whether something is an escape character or not depends on the context.
In the telecommunications field, escape characters are used to indicate that the following characters are encoded differently. This is used to alter control characters that would otherwise be noticed and acted on by the underlying telecommunications hardware, such as illegal characters. In this context, the use of escape characters is often referred to as quoting.
Definition
An escape character may not have its own meaning, so all escape sequences are of two or more characters.
Escape characters are part of the syntax for many programming languages, data formats, and communication protocols. For a given alphabet an escape character's purpose is to start character sequences (so named escape sequences), which have to be interpreted differently from the same characters occurring without the prefixed escape character.
The functions of escape sequences include:
- To encode a syntactic entity, such as device commands or special data, which cannot be directly represented by the alphabet.
- To represent characters, referred to as character quoting, which cannot be typed in the current context, or would have an undesired interpretation. In this case, an escape sequence is a digraph consisting of an escape character itself and a "quoted" character.
Control character
Generally, an escape character is not a particular case of (device) control characters, nor vice versa. If we define control characters as non-graphic, or as having a special meaning for an output device (e.g. printer or text terminal) then any escape character for this device is a control one. But escape characters used in programming (such as the backslash, "\") are graphic, hence are not control characters. Conversely most (but not all) of the ASCII "control characters" have some control function in isolation, therefore they are not escape characters.
In many programming languages, an escape character also forms some escape sequences which are referred to as control characters. For example, line break has an escape sequence of <syntaxhighlight lang="text" class="" style="" inline="1">\n</syntaxhighlight>.
Examples
JavaScript
JavaScript uses the <syntaxhighlight lang="text" class="" style="" inline="1">\</syntaxhighlight> (backslash) as an escape character for:[1][2]
- <syntaxhighlight lang="text" class="" style="" inline="1">\'</syntaxhighlight> single quote
- <syntaxhighlight lang="text" class="" style="" inline="1">\"</syntaxhighlight> double quote
- <syntaxhighlight lang="text" class="" style="" inline="1">\\</syntaxhighlight> backslash
- <syntaxhighlight lang="text" class="" style="" inline="1">\n</syntaxhighlight> new line
- <syntaxhighlight lang="text" class="" style="" inline="1">\r</syntaxhighlight> carriage return
- <syntaxhighlight lang="text" class="" style="" inline="1">\t</syntaxhighlight> tab
- <syntaxhighlight lang="text" class="" style="" inline="1">\b</syntaxhighlight> backspace
- <syntaxhighlight lang="text" class="" style="" inline="1">\f</syntaxhighlight> form feed
- <syntaxhighlight lang="text" class="" style="" inline="1">\v</syntaxhighlight> vertical tab (Internet Explorer 9 and older treats <syntaxhighlight lang="text" class="" style="" inline="1">'\v</syntaxhighlight> as <syntaxhighlight lang="text" class="" style="" inline="1">'v</syntaxhighlight> instead of a vertical tab (<syntaxhighlight lang="text" class="" style="" inline="1">'\x0B</syntaxhighlight>). If cross-browser compatibility is a concern, use <syntaxhighlight lang="text" class="" style="" inline="1">\x0B</syntaxhighlight> instead of <syntaxhighlight lang="text" class="" style="" inline="1">\v</syntaxhighlight>.)
- <syntaxhighlight lang="text" class="" style="" inline="1">\0</syntaxhighlight> null character (U+0000 NULL) (only if the next character is not a decimal digit; else it is an octal escape sequence)
- <syntaxhighlight lang="text" class="" style="" inline="1">\xFF</syntaxhighlight> character represented by the hexadecimal byte "FF"
The <syntaxhighlight lang="text" class="" style="" inline="1">\v</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">\0</syntaxhighlight> escapes are not allowed in JSON strings. Example code:<syntaxhighlight lang="javascript"> console.log("Using \\n \nWill shift the characters after \\n one row down") console.log("Using \\t \twill shift the characters after \\t one tab length to the right") console.log("Using \\r \rWill imitate a carriage return, which means shifting to the start of the row") // can be used to clear the screen on some terminals. Windows uses \r\n instead of \n alone </syntaxhighlight>
ASCII escape character
The ASCII "escape" character (octal: <syntaxhighlight lang="text" class="" style="" inline="1">\033</syntaxhighlight>, hexadecimal: <syntaxhighlight lang="text" class="" style="" inline="1">\x1B</syntaxhighlight>, or, in decimal, <syntaxhighlight lang="text" class="" style="" inline="1">27</syntaxhighlight>, also represented by the sequences <syntaxhighlight lang="text" class="" style="" inline="1">^[</syntaxhighlight> or <syntaxhighlight lang="text" class="" style="" inline="1">\e</syntaxhighlight>) is used in many output devices to start a series of characters called a control sequence or escape sequence. Typically, the escape character was sent first in such a sequence to alert the device that the following characters were to be interpreted as a control sequence rather than as plain characters, then one or more characters would follow to specify some detailed action, after which the device would go back to interpreting characters normally. For example, the sequence of <syntaxhighlight lang="text" class="" style="" inline="1">^[</syntaxhighlight>, followed by the printable characters <syntaxhighlight lang="text" class="" style="" inline="1">[2;10H</syntaxhighlight>, would cause a Digital Equipment Corporation (DEC) VT102 terminal to move its cursor to the 10th cell of the 2nd line of the screen. This was later developed into ANSI escape codes covered by the ANSI X3.64 standard. The escape character also starts each command sequence in the Hewlett-Packard Printer Command Language.
An early reference to the term "escape character" is found in Bob Bemer's IBM technical publications, who is credited with inventing this mechanism during his work on the ASCII character set.[3]
The Escape key is usually found on standard PC keyboards. However, it is commonly absent from keyboards for PDAs and other devices not designed primarily for ASCII communications. The DEC VT220 series was one of the few popular keyboards that did not have a dedicated Esc key, instead of using one of the keys above the main keypad. In user interfaces of the 1970s–1980s it was not uncommon to use this key as an escape character, but in modern desktop computers, such use is dropped. Sometimes the key was identified with AltMode (for alternative mode). Even with no dedicated key, the escape character code could be generated by typing <syntaxhighlight lang="text" class="" style="" inline="1">[</syntaxhighlight> while simultaneously holding down Ctrl.
Programming and data formats
Many modern programming languages specify the double-quote character (<syntaxhighlight lang="text" class="" style="" inline="1">"</syntaxhighlight>) as a delimiter for a string literal. The backslash (<syntaxhighlight lang="text" class="" style="" inline="1">\</syntaxhighlight>) escape character typically provides two ways to include double-quotes inside a string literal, either by modifying the meaning of the double-quote character embedded in the string (<syntaxhighlight lang="text" class="" style="" inline="1">\"</syntaxhighlight> becomes <syntaxhighlight lang="text" class="" style="" inline="1">"</syntaxhighlight>), or by modifying the meaning of a sequence of characters including the hexadecimal value of a double-quote character (<syntaxhighlight lang="text" class="" style="" inline="1">\x22</syntaxhighlight> becomes <syntaxhighlight lang="text" class="" style="" inline="1">"</syntaxhighlight>).
C, C++, Java, and Ruby all allow exactly the same two backslash escape styles. The PostScript language and Microsoft Rich Text Format also use backslash escapes. The quoted-printable encoding uses the equals sign as an escape character.
URL and URI use %-escapes to quote characters with a special meaning, as for non-ASCII characters. The ampersand (<syntaxhighlight lang="text" class="" style="" inline="1">&</syntaxhighlight>) character may be considered as an escape character in SGML and derived formats such as HTML and XML.
Some programming languages also provide other ways to represent special characters in literals, without requiring an escape character (see e.g. delimiter collision).
Communication protocols
The Point-to-Point Protocol (PPP) uses the <syntaxhighlight lang="text" class="" style="" inline="1">0x7D</syntaxhighlight> octet (<syntaxhighlight lang="text" class="" style="" inline="1">\175</syntaxhighlight>, or ASCII: <syntaxhighlight lang="text" class="" style="" inline="1">}</syntaxhighlight>) as an escape character. The octet immediately following should be XORed by <syntaxhighlight lang="text" class="" style="" inline="1">0x20</syntaxhighlight> before being passed to a higher level protocol. This is applied to both <syntaxhighlight lang="text" class="" style="" inline="1">0x7D</syntaxhighlight> itself and the control character <syntaxhighlight lang="text" class="" style="" inline="1">0x7E</syntaxhighlight> (which is used in PPP to mark the beginning and end of a frame) when those octets need to be transmitted by a higher level protocol encapsulated by PPP, as well as other octets negotiated when the link is established. That is, when a higher level protocol wishes to transmit <syntaxhighlight lang="text" class="" style="" inline="1">0x7D</syntaxhighlight>, it is transmitted as the sequence <syntaxhighlight lang="text" class="" style="" inline="1">0x7D 0x5D</syntaxhighlight>, and <syntaxhighlight lang="text" class="" style="" inline="1">0x7E</syntaxhighlight> is transmitted as <syntaxhighlight lang="text" class="" style="" inline="1">0x7D 0x5E</syntaxhighlight>.
Bourne shell
In Bourne shell (sh), the asterisk (<syntaxhighlight lang="text" class="" style="" inline="1">*</syntaxhighlight>) and question mark (<syntaxhighlight lang="text" class="" style="" inline="1">?</syntaxhighlight>) characters are wildcard characters expanded via globbing. Without a preceding escape character, an <syntaxhighlight lang="text" class="" style="" inline="1">*</syntaxhighlight> will expand to the names of all files in the working directory that do not start with a period if and only if there are such files, otherwise <syntaxhighlight lang="text" class="" style="" inline="1">*</syntaxhighlight> remains unexpanded. So to refer to a file literally called "*", the shell must be told not to interpret it in this way, by preceding it with a backslash (<syntaxhighlight lang="text" class="" style="" inline="1">\</syntaxhighlight>). This modifies the interpretation of the asterisk (<syntaxhighlight lang="text" class="" style="" inline="1">*</syntaxhighlight>). Compare:
<syntaxhighlight lang="bash"> rm * # delete all files in the current directory </syntaxhighlight> <syntaxhighlight lang="bash"> rm \* # delete the file named * </syntaxhighlight> |
Windows Command Prompt
The Windows command-line interpreter uses a caret character (<syntaxhighlight lang="text" class="" style="" inline="1">^</syntaxhighlight>) to escape reserved characters that have special meanings (in particular: <syntaxhighlight lang="text" class="" style="" inline="1">&</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">|</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">(</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">)</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1"><</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">></syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">^</syntaxhighlight>).[4] The DOS command-line interpreter, though it has similar syntax, does not support this.
For example, on the Windows Command Prompt, this will result in a syntax error. <syntaxhighlight lang="doscon" highlight="1"> C:\>echo <hello world> The syntax of the command is incorrect. </syntaxhighlight>whereas this will output the string: <syntaxhighlight lang="text" class="" style="" inline="1"><hello world></syntaxhighlight> <syntaxhighlight lang="doscon" highlight="1"> C:\>echo ^<hello world^> <hello world> </syntaxhighlight>
Windows PowerShell
In Windows, the backslash is used as a path separator; therefore, it generally cannot be used as an escape character. PowerShell uses backtick[5] ( ` ) instead.
For example, the following command: <syntaxhighlight lang="ps1con" highlight="1"> PS C:\> echo "`tFirst line`nNew line"
First line
New line </syntaxhighlight>
Others
- Quoted-printable, which encodes 8-bit data into 7-bit data of limited line lengths, uses the equals sign (<syntaxhighlight lang="text" class="" style="" inline="1">=</syntaxhighlight>) as an escape character.
See also
- AltGr key used to type characters that are unusual for the locale of the keyboard layout.
- Escape sequences in C
- Leaning toothpick syndrome
- Nested quotation
- Stropping (syntax) – in some conventions a leading character (such as an apostrophe) functions as an escape character
References
- ^ "JavaScript character escape sequences". Mathias Bynens. 21 December 2011. Retrieved 2014-06-30.
- ^ "Special Characters (JavaScript)". Microsoft Developer Network. Archived from the original on Dec 14, 2014. Retrieved 2014-06-30.
- ^ Bemer, Bob (Oct 25, 2003). "How Bob Bemer Invented the ESCAPE Sequence and Key". Bob Bemer. Archived from the original on 4 January 2018. Retrieved 22 March 2018.
- ^ Tim Hill (1998). "The Windows NT Command Shell". Microsoft Learn. MacMillan Technical Publishing. Retrieved 2010-01-13.
- ^ "about_Escape_Characters". Microsoft Developer Network. 2014-05-08. Archived from the original on 2016-11-25. Retrieved 2016-11-24.
External links
- That Powerful ESCAPE Character -- Key and Sequences Archived 2016-03-25 at the Wayback Machine – Bob Bemer
This article incorporates public domain material from Federal Standard 1037C. General Services Administration. Archived from the original on 2022-01-22.