Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Escape sequence
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{about|sequences of characters that, because of a prefix, have a special meaning, possibly to control peripheral devices|specialized usages|Escape sequence (disambiguation)}} {{Short description|Character combinations with ulterior meaning}} {{Use American English|date=March 2019}} In [[computer science]], an '''escape sequence''' is a combination of [[Character (computing)|characters]] that has a meaning other than the literal characters contained therein;<ref>{{cite web |title=Escape Sequence |url=https://www.spss-tutorials.com/escape-sequence}}</ref> it is marked by one or more preceding (and possibly terminating) characters.<ref>{{cite web |title=Characters |work=The Java Tutorials |url=https://docs.oracle.com/javase/tutorial/java/data/characters.html}}</ref> ==Examples== * In [[C (programming language)|C]] and many derivative programming languages, a string escape sequence is a series of two or more characters, [[Escape sequences in C|starting with a backslash <code>\</code>]].<ref>{{cite web |quote=Character combinations consisting of a backslash <code>\</code> followed by a letter or by a combination of digits are called ''escape sequences''. |title=Escape Sequences |date=3 August 2021 |url=https://msdn.microsoft.com/en-us/library/h21280bw.aspx}}</ref> ** Note that in C a backslash immediately followed by a newline does <em>not</em> constitute an escape sequence, but splices physical source lines into logical ones in the second translation phase, whereas string escape sequences are converted in the fifth translation phase.<ref>{{cite web |url=http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf#page=29 |title=ISO/IEC 9899:201x Committee Draft N1570 |language=English |quote=5.1.1.2 Translation phases, 2.: Each instance of a backslash character (<code>\</code>) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. [...]}}</ref> ** To represent the backslash character itself, <code>\\</code> can be used, whereby the first backslash indicates an escape and the second specifies that a backslash is being escaped.<ref>{{cite web |url=https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/escape.htm |title=Escape sequences|website=[[IBM]] }}</ref> ** A character may be escaped in multiple different ways. Assuming ASCII encoding, the escape sequences <code>\x5c</code> ([[hexadecimal]]), <code>\\</code>, and <code>\134</code> ([[octal]]) all encode the same character: the backslash <code>\</code>. * For devices that respond to [[ANSI escape code|ANSI escape]] sequences, the combination of three or more characters beginning with the ASCII "escape" character (decimal character code 27) followed by the left-bracket character <code>[</code> (decimal character code 91) defines an escape sequence. ==Control sequences== When directed, this series of [[character (computing)|characters]] is used to change the [[State (computer science)|state]] of [[computer]]s and their attached [[peripheral]] devices, rather than to be displayed or printed as regular [[Data (computing)|data]] bytes would be, these are also known as '''control sequences''', reflecting their use in device control, beginning with the '''Control Sequence Initiator''' - originally the "escape character" ASCII code - character 27 (decimal) - often written "Esc" on [[keycap]]s. With the introduction of ANSI terminals most escape sequences began with the ''two'' characters "ESC" then "[" or a specially-allocated '''CSI''' character with a code 155 (decimal). Not all control sequences used an escape character; for example: * modem control sequences used by AT/[[Hayes command set|Hayes-compatible]] modems<ref name="Hayes"/><ref name="CISCO"/> * [[Data General]] terminal control sequences,<ref name="Data_General_Terminals"/><ref name="Kermit"/><ref name="DG210"/> but they often were still called escape sequences, and the very common use of "escaping" special characters in programming languages and command-line parameters today often use the "backslash" character to begin the sequence. Escape sequences in communications are commonly used when a computer and a peripheral have only a single channel through which to send information back and forth (so escape sequences are an example of [[in-band signaling]]).<ref name="Dict"/><ref name="Terminal_Handbook"/> They were common when most [[dumb terminals]] used [[ASCII]] with 7 data bits for communication, and sometimes would be used to switch to a different character set for "foreign" or graphics characters that would otherwise been restricted by the 128 codes available in 7 data bits. Even relatively "dumb" terminals responded to some escape sequences, including the original mechanical Teletype printers (on which "glass Teletypes" or VDUs were based) responded to characters 27 and 31 to alternate between letters and figures modes. ==Keyboard== An escape character is usually assigned to the [[Esc key]] on a [[computer keyboard]], and can be sent in other ways than as part of an escape sequence. For example, the Esc key may be used as an input character in editors such as [[Vi (text editor)|vi]],<ref name="VI"/> or for backing up one level in a menu in some applications.<ref name="PCWorld_2009"/> The Hewlett Packard [[HP 2640]] terminals had a key for a "display functions" mode which would display graphics for all control characters, including Esc, to aid in [[debugging]] applications. If the Esc key and other keys that send escape sequences are both supposed to be meaningful to an application, an ambiguity arises if a [[character terminal]] is in use. When the application receives the [[ASCII]] escape character, it is not clear whether that character is the result of the user pressing the Esc key or whether it is the initial character of an escape sequence (e.g., resulting from an arrow key press). The traditional method of resolving the ambiguity is to observe whether or not another character quickly follows the escape character. If not, it is assumed not to be part of an escape sequence. This [[heuristic]] can fail under some circumstances, especially without fast modern communication speeds. Escape sequences date back at least to the 1874 [[Baudot code]].<ref name="Economist_2013"/><ref name="Baudot"/><ref name="TC304"/> ==Modem control== The [[Hayes command set]], for instance, defines a single escape sequence, ''[[+++ (modem)|+++]]''. (In order to interpret ''+++'', which may be a part of data, as the escape sequence, the sender stops communication for one second before and after the ''+++''.) When the modem encounters this in a stream of data, it switches from its normal mode of operation, which simply sends any characters to the phone, to a command mode in which the following data is assumed to be a part of the command language. You can switch back to the ''online mode'' by sending the O command. The Hayes command set is [[Mode (user interface)|modal]], switching from command mode to online mode.<ref name="Modem_2011"/><ref name="Modem_Programming"/> This is not appropriate in the case where the commands and data will switch back and forth rapidly. An example of a non-modal escape sequence control language is the [[VT100]], which used a series of commands prefixed by a [[Control Sequence Introducer]]. ==Comparison with control characters== {{main|Control character}} A control character is a character that, in isolation, has some control function, such as [[carriage return]] (CR). Escape sequences, by contrast, consist of one or more [[escape character]]s which change the interpretation of subsequent characters. ==ASCII video data terminals== The [[VT52]] terminal used simple [[Digraph (computing)|digraph]] commands like escape-A: in isolation, "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 Hewlett Packard [[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. ===Use in DOS and Windows=== A utility, [[ANSI.SYS]],<ref>{{cite book |title=17. Understanding ANSI.SYS - Special Edition Using MS-DOS 6.22 |url=https://www.oreilly.com/library/view/special-edition-using/0789725738/ch17.html}}</ref> can be used to enable the interpreting of the ANSI (ECMA-48) terminal escape sequences under [[DOS]] (by using <code>$e</code> in the [[PROMPT (DOS command)|PROMPT]] command) or in command windows in 16-bit [[Windows]]. The rise of [[GUI]] applications, which directly write to display cards, has greatly reduced the usage of escape sequences on Microsoft platforms, but they can still be used to create interactive random-access character-based screen interfaces with the character-based library routines such as [[printf]] without resorting to a GUI program. ===Use in Linux and Unix displays=== The default text terminal, and text windows (such as using [[xterm]]) respond to ANSI escape sequences. ==Quoting escape== ===Overview=== When an [[escape character]] is needed within the quoted/escaped string, there are two strategies used within programming and scripting languages: * doubled delimiter (e.g. <code><nowiki>'He didn''t do it.'</nowiki></code>)<ref name="IEY"/> * secondary escape sequence An example of the latter is in the use of the caret (<code>^</code>). E.g. this outputs "You can do so via Cut&Paste" in [[cmd.exe|CMD]]. (otherwise, the ampersand has a restricted use)<ref name="CMD.a"/> echo You can do so via Cut^&Paste ===In detail=== {{see|String literal#Escape sequences}} {{see also|Escape sequences in C}} A common use of escape sequences is in fact to remove control characters found in a binary data stream so that they will not cause their control function by mistake. In this case, the control character is replaced by a defined "escape character" (which need not be the US-ASCII escape character) and one or more other characters; after exiting the context where the control character would have caused an action, the sequence is recognized and replaced by the removed character.<ref name="CMD.a"/> To transmit the "escape character" itself, two copies are sent.<ref name="IEY"/> In many [[programming language]]s and command line interfaces escape sequences are used in [[character literal]]s and [[string literal]]s, to express characters which are not printable or clash with the syntax of characters or strings. For example, [[control characters]] themselves might not be allowed to be placed in the program coded by the editor program, or may have undesirable side-effects if typed into a command. The end-of-quote character is also a problem for programmers that can be solved by escaping it. In most contexts the escape character is the [[backslash]] ("'''\'''"). ===Samples=== For example, the single quotation mark character might be expressed as <code><nowiki>'\''</nowiki></code> since writing <code><nowiki>'''</nowiki></code> is not acceptable. Many modern programming languages specify the doublequote character (<code><nowiki>"</nowiki></code>) as a [[delimiter]] for a string literal. The backslash escape character typically provides ways to include doublequotes inside a string literal, such as by modifying the meaning of the doublequote character embedded in the string (<code><nowiki>\"</nowiki></code>), or by modifying the meaning of a sequence of characters including the hexadecimal value of a doublequote character (<code><nowiki>\x22</nowiki></code>). Both sequences encode a literal doublequote (<code><nowiki>"</nowiki></code>). In [[Perl]] or [[Python (programming language)|Python]] 2 <syntaxhighlight lang="perl"> print "Nancy said "Hello World!" to the crowd."; </syntaxhighlight> produces a syntax error, whereas: <syntaxhighlight lang="perl"> print "Nancy said \"Hello World!\" to the crowd."; ### example of \" </syntaxhighlight> produces the intended output. Another alternative: <syntaxhighlight lang="perl"> print "Nancy said \x22Hello World!\x22 to the crowd."; ### example of \x22 </syntaxhighlight> uses "\x" to indicate the following two characters are hexadecimal digits, "22" being the ASCII value for a doublequote in hexadecimal. [[C (programming language)|C]], [[C++]], [[Java (programming language)|Java]], and [[Ruby (programming language)|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 [[percent-encoding]] to quote characters with a special meaning, as for non-ASCII characters. Another similar (and partially overlapping) syntactic trick is [[stropping (syntax)|stropping]]. Some programming languages also provide other ways to represent special characters in literals, without requiring an escape character (see e.g. [[delimiter collision]]). ==See also== * [[Control character]] * [[Escape character]] * [[printf format string]] * [[Format (Common Lisp)|format control string]] ==References== {{reflist|refs= <ref name="Hayes">{{cite web |title=Chapter 5 β AT Commands |url=https://www.perle.com/support_services/documentation_pdfs/5500158.pdf}}</ref> <ref name="CISCO">{{cite web |url=https://www.cisco.com/c/en/us/td/docs/routers/access/2600/software/notes/analogat.html |title=AT Command Set and Register Summary for Analog Modem Modules}}</ref> <ref name="Data_General_Terminals">{{Cite FTP |title=Data General terminals: discussion of |server=FTP server |url-status=dead |url=ftp://ftp.invisible-island.net/shuford/terminal/data_general_news.txt}}</ref> <ref name="Kermit">{{cite web |title=What's a Terminal? |url=http://www.kermitproject.org/terminals.html}}</ref> <ref name="DG210">{{cite web |title=Data General DG210 DG211 Terminal Emulation Software |url=https://www.hilgraeve.com/knowledge_base/dg210-dg211-emulation}}</ref> <ref name="Dict">{{cite web |url=https://www6.dict.cc/wp_examples.php?lp_id=1%26lang=en%26s=escape%2520sequence |title=Escape sequence}}</ref> <ref name="Terminal_Handbook">{{cite web |title=Terminals & Printers Handbook Glossary |url=https://vt100.net/docs/tp83/glossary.html}}</ref> <ref name="VI">{{cite web |quote=vi commands [β¦] Pressing the Esc (Escape) key is how you [β¦] |title=Twelve Useful "vi" Commands |url=http://www.eng.buffalo.edu/~yearke/unix/vi12.shtml}}</ref> <ref name="PCWorld_2009">{{cite magazine |magazine=[[PCworld]] |date=2009-10-29 |title=Five Unexpected Uses for the Esc Key |url=https://www.pcworld.com/article/174661/article.html}}</ref> <ref name="Economist_2013">{{cite magazine |magazine=[[The Economist]] |date=2013-06-09 |url=https://www.economist.com/the-economist-explains/2013/06/09/what-is-ascii |title=What is ASCII? The Economist explains}}</ref> <ref name="Baudot">{{cite web |quote=The Baudot code, invented in 1870 and patented in 1874 by J. Baudot is [β¦] |title=Baudot and CCITT code |url=http://rabbit.eng.miami.edu/info/baudot.html}}</ref> <ref name="TC304">{{cite web |quote=elements C0 and C1 of control characters [β¦] a 5-bit code patented by Jean-Maurice-Emile Baudot (1845-1903) in 1874 |url=http://www.open-std.org/CEN/TC304/guidecharactersets/Annexa.doc |title=Guide to the use of Character Sets in Europe}}</ref> <ref name="Modem_2011">{{cite web |quote=+++ - "Escape Sequence" - This command initiates an escape sequence to return the modem to the on-line command mode |title=Basic Hayes AT Command Set |url=http://www.modemhelp.net/basicatcommand.shtml |date=2011-02-05}}</ref> <ref name="Modem_Programming">{{cite web |quote=When a modem is in command mode, the modem can accept commands from you |title=Modem Programming Basics |url=https://www.activexperts.com/serial-port-component/tutorials/modem}}</ref> <ref name="IEY">{{cite web |quote=Within the field, two consecutive apostrophes [β¦] |title=Apostrophe Editing ('aaa') (FORTRAN 77 Language Reference) |url=https://docs.oracle.com/cd/E19957-01/805-4939/z40007438613/index.html}}</ref> <ref name="CMD.a">{{cite web |url=https://technet.microsoft.com/en-us/library/cc723564.aspx |title=The Windows NT Command Shell|date=20 February 2014 }}</ref> }} {{DEFAULTSORT:Escape Sequence}} [[Category:Control characters]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Main
(
edit
)
Template:Reflist
(
edit
)
Template:See
(
edit
)
Template:See also
(
edit
)
Template:Short description
(
edit
)
Template:Use American English
(
edit
)