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 character
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!
{{short description |Character used to quote special-purpose characters, e.g. '\'}} {{for|escaping markup in Wikipedia edits|WP:NOWIKI|selfref=y}} {{Distinguish|control character|escape sequence|Escape key}} {{Refimprove|date=April 2010}} In [[computing]] and [[telecommunications]], an '''escape character''' is a [[character (computing)|character]] that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of [[metacharacter]]s. 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 character]]s that would otherwise be noticed and acted on by the underlying telecommunications hardware, such as [[illegal character]]s. 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 [[formal syntax|syntax]] for many programming languages, data formats, and communication protocols. For a given [[alphabet (computer science)|alphabet]] an escape character's purpose is to start character sequences (so named [[escape sequence]]s), 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 (computing)|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 character]]s, nor vice versa. If we define control characters as non-[[graphic character|graphic]], or as having a special meaning for an output device (e.g. [[computer printer|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 (computing)|line break]] has an escape sequence of {{code|\n}}. ==Examples== ===JavaScript === JavaScript uses the {{code|\}} (backslash) as an escape character for:<ref>{{cite web|url=http://mathiasbynens.be/notes/javascript-escapes |title=JavaScript character escape sequences |publisher=Mathias Bynens |date=21 December 2011 |access-date=2014-06-30}}</ref><ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/2yfce773%28v=vs.94%29.aspx |title=Special Characters (JavaScript) |publisher=Microsoft Developer Network |access-date=2014-06-30 |url-status=live |archive-url=https://web.archive.org/web/20141214014640/http://msdn.microsoft.com/en-us/library/2yfce773(v=vs.94).aspx |archive-date= Dec 14, 2014 }}</ref> * {{code|\'}} single quote * {{code|\"}} double quote * {{code|\\}} backslash * {{code|\n}} [[newline|new line]] * {{code|\r}} [[carriage return]] * {{code|\t}} tab * {{code|\b}} backspace * {{code|\f}} [[form feed]] * {{code|\v}} [[vertical tab]] ([[Internet Explorer 9]] and older treats {{code|'\v}} as {{code|'v}} instead of a vertical tab ({{code|'\x0B}}). If cross-browser compatibility is a concern, use {{code|\x0B}} instead of {{code|\v}}.) * {{code|\0}} [[null character]] ({{unichar|0}}) (only if the next character is not a decimal digit; else it is an octal escape sequence) * {{code|\xFF}} character represented by the hexadecimal byte <code>FF</code> The {{code|\v}} and {{code|\0}} 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]]: {{code|\033}}, [[hexadecimal]]: {{code|\x1B}}, or, in decimal, {{code|27}}, also represented by the sequences {{code|^[}} or {{code|\e}}) 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 {{code|^[}}, followed by the printable characters {{code|[2;10H}}, would cause a Digital Equipment Corporation (DEC) [[VT102]] terminal to move its [[cursor (computers)|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.<ref>{{cite web|url=http://www.bobbemer.com/ESCAPE.HTM|title=How Bob Bemer Invented the ESCAPE Sequence and Key|first=Bob|last=Bemer|website=Bob Bemer |date=Oct 25, 2003 |access-date=22 March 2018|archive-date=4 January 2018|archive-url=https://web.archive.org/web/20180104163857/http://www.bobbemer.com/ESCAPE.HTM|url-status=dead}}</ref> 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 interface]]s 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 {{code|[}} while simultaneously holding down {{Key press|[[Control key|Ctrl]]}}. ===Programming and data formats=== Many modern [[programming language]]s specify the double-quote character ({{code|"}}) as a [[delimiter]] for a [[string literal]]. The [[backslash]] ({{code|\}}) 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 ({{code|\"}} becomes {{code|"}}), or by modifying the meaning of a sequence of characters including the hexadecimal value of a double-quote character ({{code|\x22}} becomes {{code|"}}). [[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|escapes]] to quote characters with a special meaning, as for non-ASCII characters. The [[ampersand]] ({{code|&}}) 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 {{code|0x7D}} [[octet (computing)|octet]] ({{code|\175}}, or ASCII: <code>}</code>) as an escape character. The octet immediately following should be [[XOR]]ed by {{code|0x20}} before being passed to a higher level protocol. This is applied to both {{code|0x7D}} itself and the control character {{code|0x7E}} (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 {{code|0x7D}}, it is transmitted as the sequence {{code|0x7D 0x5D}}, and {{code|0x7E}} is transmitted as {{code|0x7D 0x5E}}. ===Bourne shell=== In [[Bourne shell]] (sh), the [[asterisk]] ({{code|*}}) and [[question mark]] ({{code|?}}) characters are [[wildcard character]]s expanded via [[globbing]]. Without a preceding escape character, an {{code|*}} 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 {{code|*}} 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 ({{code|\}}). This modifies the interpretation of the asterisk ({{code|*}}). Compare: {| |- | style="width:25px;"| <!--indented-->|| <syntaxhighlight lang="bash"> rm * # delete all files in the current directory </syntaxhighlight> <syntaxhighlight lang="bash"> rm \* # delete the file named * </syntaxhighlight> |} Similarly, characters like the [[ampersand]], [[Vertical bar|pipe]] and [[semicolon]] (used for command chaining), angle brackets (used for [[Redirection (computing)|redirection]]), and parentheses have special syntactic meaning to the Bourne shell. These must also be escaped—referred to as "quoting" in the {{code|sh(1)}} manual page<ref>https://heirloom.sourceforge.net/sh/sh.1.html#8</ref>—in order to be used literally as arguments to another program: <syntaxhighlight lang="console"> $ echo (`-´)> # not escaped or quoted bash: syntax error near unexpected token ``-´' $ echo \(`-´\)\> # escaped with backslashes (`-´)> $ echo '(`-´)>' # protected by single quotes; same effect as above (`-´)> $ echo ;) # syntax error $ echo ';)' \;\) # both OK </syntaxhighlight> ===Windows Command Prompt=== The [[cmd.exe|Windows command-line interpreter]] uses a [[caret]] character ({{code|^}}) to escape reserved characters that have special meanings (in particular: {{code|&}}, <code>|</code>, {{code|(}}, {{code|)}}, {{code|<}}, {{code|>}}, {{code|^}}).<ref>{{cite web |url=https://technet.microsoft.com/en-us/library/cc723564.aspx |website=Microsoft Learn |title=The Windows NT Command Shell |author=Tim Hill |year=1998 |publisher=MacMillan Technical Publishing |access-date=2010-01-13}}</ref> The [[COMMAND.COM|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: {{code|<hello world>}} <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]]<ref>{{cite web |url=https://technet.microsoft.com/en-us/library/hh847755.aspx |title=about_Escape_Characters |publisher=Microsoft Developer Network |date=2014-05-08 |access-date=2016-11-24 |archive-url=https://web.archive.org/web/20161125044021/https://technet.microsoft.com/en-us/library/hh847755.aspx |archive-date=2016-11-25 |url-status=dead }}</ref> ( ` ) 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]] (<code>=</code>) 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== <references/> ==External links== *[http://www.bobbemer.com/ESCAPE.HTM That Powerful ESCAPE Character -- Key and Sequences] {{Webarchive|url=https://web.archive.org/web/20160325190301/http://www.bobbemer.com/ESCAPE.HTM |date=2016-03-25 }} – [[Bob Bemer]] {{FS1037C}} [[Category:Pattern matching]] [[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:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Distinguish
(
edit
)
Template:FS1037C
(
edit
)
Template:For
(
edit
)
Template:Key press
(
edit
)
Template:Refimprove
(
edit
)
Template:Short description
(
edit
)
Template:Unichar
(
edit
)
Template:Webarchive
(
edit
)