Escape character
This article needs additional citations for verification. (April 2010) |
In
In the
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
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 digraphconsisting of an escape character itself and a "quoted" character.
Control character
Generally, an escape character is not a particular case of (device)
In many programming languages, an escape character also forms some escape sequences which are referred to as control characters. For example,
\n
.
Examples
JavaScript
JavaScript uses the \
(backslash) as an escape character for:[1][2]
\'
single quote\"
double quote\\
backslash\n
new line\r
carriage return\t
tab\b
backspace\f
form feed\v
vertical tab (Internet Explorer 9 and older treats'\v
as'v
instead of a vertical tab ('\x0B
). If cross-browser compatibility is a concern, use\x0B
instead of\v
.)\0
null character (U+0000 NULL) (only if the next character is not a decimal digit; else it is an octal escape sequence)\xFF
character represented by the hexadecimal byte "FF"
The \v
and \0
escapes are not allowed in JSON strings.
Example code:
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
ASCII escape character
The ASCII "escape" character (
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
[
while simultaneously holding down CtrlProgramming and data formats
Many modern programming languages specify the double-quote character ("
) as a delimiter for a string literal. The backslash (\
) 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 (\"
becomes "
), or by modifying the meaning of a sequence of characters including the hexadecimal value of a double-quote character (\x22
becomes "
).
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.
.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
0x20
before being passed to a higher level protocol. This is applied to both 0x7D
itself and the control character 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 0x7D
, it is transmitted as the sequence 0x7D 0x5D
, and 0x7E
is transmitted as 0x7D 0x5E
.
Bourne shell
In Bourne shell (sh), the asterisk (*
) and question mark (?
) characters are wildcard characters expanded via globbing. Without a preceding escape character, an *
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 *
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 (\
). This modifies the interpretation of the asterisk (*
). Compare:
rm * # delete all files in the current directory
rm \* # delete the file named *
|
Windows Command Prompt
The Windows command-line interpreter uses a caret character (^
) to escape reserved characters that have special meanings (in particular: &
, |
, (
, )
, <
, >
, ^
).[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.
C:\>echo <hello world>
The syntax of the command is incorrect.
whereas this will output the string: <hello world>
C:\>echo ^<hello world^>
<hello world>
Windows PowerShell
In
For example, the following command:
PS C:\> echo "`tFirst line`nNew line"
First line
New line
Others
- Quoted-printable, which encodes 8-bit data into 7-bit data of limited line lengths, uses the equals sign (
=
) 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.