Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Escape character

From Wikipedia, the free encyclopedia
Character used to start an escape sequence
For escaping markup in Wikipedia edits, seeWP:NOWIKI.
Not to be confused withcontrol character,escape sequence, orEscape key.
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Escape character" – news ·newspapers ·books ·scholar ·JSTOR
(April 2010) (Learn how and when to remove this message)

Incomputing andtelecommunications, anescape character is acharacter (more specifically ametacharacter) that, based on a contextual convention, specifies an alternative interpretation of the sequence of characters that follow it. The escape character plus the characters that follow it to form asyntactic unit is called anescape 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 asASCIIescape.

In telecommunications, an escape character is used to indicate that the following characters are encoded differently. This is used to altercontrol characters that would otherwise be noticed and acted on by the underlying telecommunications hardware, such asillegal characters. In this context, the use of an escape character is sometimes referred to asquoting.

Definition

[edit]

An escape character may not have its own meaning, so all escape sequences are of two or more characters.

Escape characters are part of thesyntax for many programming languages, data formats, and communication protocols. For a givenalphabet an escape character's purpose is to start character sequences (so namedescape 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 ascharacter quoting, which cannot be typed in the current context, or would have an undesired interpretation. In this case, an escape sequence is adigraph consisting of an escape character itself and a "quoted" character.

Control character

[edit]

In contrast to an escape character, acontrol character (i.e.carriage return) has meaning on its own, without a special prefix or following characters. An escape character has no meaning on its own. It only has meaning in the context of a sequence.

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 ortext terminal) then any escape character for this device is a control one. But escape characters used in programming (such as thebackslash,\) are graphic, hence are not control characters. Conversely most (but not all) of theASCII "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\n.

Examples

[edit]

JavaScript

[edit]

JavaScript uses the\ (backslash) as an escape character for:[1][2]

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

A similar list is used by several other programming languages.

ASCII escape character

[edit]

The ASCII "escape" character (octal:\033,hexadecimal:\x1B, or, in decimal,27, also represented by the sequences^[ or\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^[, followed by the printable characters[2;10H, would cause a Digital Equipment Corporation (DEC)VT102 terminal to move itscursor to the 10th cell of the 2nd line of the screen. This was later developed intoANSI escape codes covered by the ANSI X3.64 standard. The escape character also starts each command sequence in the Hewlett-PackardPrinter Command Language.

An early reference to the term "escape character" is found inBob Bemer's IBM technical publications, who is credited with inventing this mechanism during his work on theASCII character set.[3]

TheEscape 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 DECVT220 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. Inuser 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 withAltMode (for alternative mode). Even with no dedicated key, the escape character code could be generated by typing[ while simultaneously holding downCtrl.

Programming and data formats

[edit]

Many modernprogramming languages specify the double-quote character (") as adelimiter for astring literal. Thebackslash (\) 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"). Some languages such as Pascal and Python allow single-quote character (') as string literal delimiter.

C,C++,Java, andRuby all allow exactly the same two backslash escape styles. ThePostScript language and MicrosoftRich Text Format also use backslash escapes. Thequoted-printable encoding uses theequals sign as an escape character.

URL andURI use%-escapes to quote characters with a special meaning, as for non-ASCII characters. Theampersand (&) character may be considered as an escape character inSGML and derived formats such asHTML andXML.

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

[edit]

ThePoint-to-Point Protocol (PPP) uses the0x7Doctet (\175, or ASCII:}) as an escape character. The octet immediately following should beXORed by0x20 before being passed to a higher level protocol. This is applied to both0x7D itself and the control character0x7E (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 transmit0x7D, it is transmitted as the sequence0x7D 0x5D, and0x7E is transmitted as0x7D 0x5E.

Bourne shell

[edit]

InBourne shell (sh), theasterisk (*) andquestion mark (?) characters arewildcard characters expanded viaglobbing. Without a preceding escape character, an* will expand to the names of all files in theworking directory that do not start with a periodif 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 *

Similarly, characters like theampersand,pipe andsemicolon (used for command chaining), angle brackets (used forredirection), and parentheses have special syntactic meaning to the Bourne shell. These must also be escaped—referred to as "quoting" in thesh(1) manual page[4]—in order to be used literally as arguments to another program:

$echo(`-´)># not escaped or quotedbash: syntax error near unexpected token ``-´'$echo\(`-´\)\># escaped with backslashes(`-´)>$echo'(`-´)>'# protected by single quotes; same effect as above(`-´)>$echo;)# syntax error$echo';)'\;\)# both OK

Windows Command Prompt

[edit]

TheWindows command-line interpreter uses acaret character (^) to escape reserved characters that have special meanings (in particular:&,|,(,),<,>,^).[5] TheDOS 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

[edit]

InWindows, the backslash is used as a path separator; therefore, it generally cannot be used as an escape character.PowerShell usesbacktick[6] ( ` ) instead.

For example, the following command:

PS C:\>echo"`tFirst line`nNew line"        First lineNew line

Others

[edit]

Summary

[edit]

These characters are mentioned as used as escape characters or string literal delimiters.

  •  \x1B ASCII escape character
  • "\x22 String literal delimiter
  • %\x25 Percent-encoding
  • &\x26 SGML, HTML and XML
  • '\x27 String literal delimiter
  • =\x3D Quoted-printable
  • \\x5C Many programming languages
  • `\x60 Windows PowerShell
  • }\x7D Point-to-Point Protocol
and more occur for this purpose, such as$,#,*,+,;,<,>,? ,@ and~

See also

[edit]

References

[edit]
  1. ^"JavaScript character escape sequences". Mathias Bynens. 21 December 2011. Retrieved2014-06-30.
  2. ^"Special Characters (JavaScript)". Microsoft Developer Network.Archived from the original on Dec 14, 2014. Retrieved2014-06-30.
  3. ^Bemer, Bob (Oct 25, 2003)."How Bob Bemer Invented the ESCAPE Sequence and Key".Bob Bemer. Archived fromthe original on 4 January 2018. Retrieved22 March 2018.
  4. ^"Manual Page - sh(1)".
  5. ^Tim Hill (1998)."The Windows NT Command Shell".Microsoft Learn. MacMillan Technical Publishing. Retrieved2010-01-13.
  6. ^"about_Escape_Characters". Microsoft Developer Network. 2014-05-08. Archived fromthe original on 2016-11-25. Retrieved2016-11-24.

External links

[edit]

Public Domain This article incorporatespublic domain material fromFederal Standard 1037C.General Services Administration. Archived fromthe original on 2022-01-22.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Escape_character&oldid=1337833277"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp