The U+001B Character: What the ESC Control Code Means in Text
What U+001B (ESC) is, why it shows up as \u001b or ^[ in logs and JSON, and whether to keep, escape, visualize or strip it.
If you see \u001b, \x1b, \033 or ^[ in a log, a JSON payload or pasted terminal output, you are looking at one invisible character: U+001B, the ESCAPE control code. It has no glyph of its own. Its job is to tell a terminal or printer that the next few characters are a command, not text. This guide explains what the character does, how it differs from printable text, and how to decide whether to keep it, escape it, show it or remove it.
Short answer: what to do with U+001B
Keep ESC only where a terminal or terminal-compatible viewer will interpret it, such as colored command-line output shown live. Everywhere else, including documentation, tickets, chat, web pages, search indexes and text meant for screen readers, a plain-text version without escape sequences is usually the better choice. The benefit of keeping ESC is formatting like color and bold in terminals. The cost is that outside a terminal the sequences become clutter or invisible noise, and they can break search and editing. If you need to keep the character for data fidelity, store it in escaped form, such as \u001b in JSON, rather than as a raw byte.
What U+001B is
U+001B is a C0 control character inherited from ASCII, where it has the value 27 (hexadecimal 1B). Unicode classifies it as a control character (general category Cc). Its formal name field is just <control>, with the alias ESCAPE and the abbreviation ESC. It is the same character the Esc key sends in most terminals, and Ctrl+[ produces it as well. That is where the caret notation ^[ comes from.
Text written as \u001b is not the character itself. It is a six-character escape notation that JSON, JavaScript, Java and similar formats use to represent ESC safely inside a string. A JSON string cannot contain raw control characters in the range U+0000 to U+001F, so serializers write ESC as \u001b. When the JSON is parsed, those six characters become a single ESC character again.
| Notation | Where you see it |
| U+001B | Unicode code point reference |
| 27 / 0x1B / 033 | Decimal, hexadecimal and octal values |
| ^[ | Caret notation, for example in the output of cat -v |
| \x1b or \033 | C, Python, shell printf and many regex engines |
| \e | Some shells and printf implementations (not universal) |
| \u001b | JSON, JavaScript, Java string escapes |
| ␛ (U+241B) | SYMBOL FOR ESCAPE, a printable stand-in for showing the control visually |
What an escape sequence changes
ESC usually starts a sequence defined by the ECMA-48 / ISO 6429 family of standards, often called ANSI escape codes. The most common type is the Control Sequence Introducer: ESC followed by [, optional parameters, and a final letter. For example, ESC[31m switches the foreground to red, ESC[1m turns on bold, and ESC[0m resets formatting. Other sequences move the cursor, clear the screen or set the terminal window title. Window-title sequences start with ESC] instead of ESC[.
This is a presentation instruction, not a change to the visible letters. In a terminal, the text Error printed in red is still stored as the bytes ESC [ 3 1 m E r r o r ESC [ 0 m. The terminal reads the sequence and acts on it. Any program that does not interpret it, such as a text editor, a web page or a word processor, receives the full string, control code included.
"Reverse" has a specific meaning here that can be confused with text reversal tools. The sequence ESC[7m sets reverse video, which swaps foreground and background colors. It does not reverse character order, mirror glyphs or substitute Unicode lookalike letters. Likewise, the bold and color effects of ANSI codes are terminal rendering attributes. They are not fonts, and they do not survive outside a terminal the way Unicode styled letters (for example, mathematical bold characters) would.
How to find invisible ESC characters
Because ESC has no glyph, the most reliable check is to make it visible with a tool that shows control characters instead of interpreting them. The commands below are standard on Linux and macOS shells. The output shown follows from each tool's documented behavior: cat -v prints ESC as ^[, and Python's repr prints it as \x1b.
$ printf '\033[31mError\033[0m\n' | cat -v
^[[31mError^[[0m
$ printf '\033[31mError\033[0m' | hexdump -C
# ESC appears as the byte 1b
>>> s = "\x1b[31mError\x1b[0m"
>>> print(repr(s))
'\x1b[31mError\x1b[0m'
>>> import json; json.dumps(s)
'"\\u001b[31mError\\u001b[0m"'
Common signs that a file contains raw escape sequences include fragments like [31m or [0m scattered through otherwise normal text. This happens because many editors and web pages drop or hide the ESC itself and display only the printable remainder. Another sign is a search for a word that fails even though the word looks present, because an invisible ESC sequence sits inside it.
Will it survive copying and sharing?
- Copying from a terminal window: terminal emulators generally copy the rendered characters, not the escape sequences, so colored output usually pastes as plain text. Behavior varies by emulator and its settings, and was not tested here.
- Redirecting output to a file: the raw sequences are written to the file. Many command-line tools disable color when output is not a terminal, but not all do, so logs captured this way often contain ESC.
- Opening in an editor or browser: the sequences usually appear as stray fragments like [32m, or as an invisible character. They do not appear as color.
- Viewing with a pager: less shows control characters by default instead of interpreting them. Its -R option passes color sequences through to the terminal.
- Embedding in structured formats: JSON requires escaping ESC as \u001b. XML 1.0 does not allow U+001B in documents at all, even as a character reference. HTML treats control characters as parse errors, so behavior depends on the parser.
For accessibility, the safe assumption is that escape sequences carry no meaning for assistive technology. Color or bold applied through ANSI codes is a visual cue only. If it signals something important, such as an error versus a warning, the text should say so in words. No screen reader testing was available for this article, so how a specific screen reader announces leftover fragments like [31m is unverified. Expect that, at best, the fragments are skipped and, at worst, read aloud as noise.
Escape sequences in untrusted text also deserve care. Printing user-supplied or logged content directly to a terminal lets embedded sequences change colors, move the cursor, overwrite earlier lines or change the window title. This can hide or disguise content. Showing such text with cat -v, or stripping control characters before display, avoids that.
Choosing a transformation
- Keep raw ESC: only for output that a terminal will render, such as live CLI color.
- Escape it (\u001b, \x1b): when the exact original bytes must be preserved inside JSON, source code or test fixtures. Meaning is preserved, and the string stays valid and inspectable.
- Visualize it (^[ or ␛): when debugging or reviewing text for hidden characters. This is good for inspection, but the result is no longer the original data.
- Strip it: for documentation, tickets, chat, web display, search indexing and accessible plain text. Readability improves, but formatting is lost, and any meaning carried only by color is lost unless it is restated in words.
Stripping usually means removing complete escape sequences, not just the ESC character. If you delete only ESC, fragments like [31m remain in the text. The pattern below removes standard CSI sequences, which cover colors, bold and cursor movement. It follows the ECMA-48 structure: parameter bytes 0x30 to 0x3F, intermediate bytes 0x20 to 0x2F, and a final byte 0x40 to 0x7E.
import re
CSI = re.compile(r'\x1b\[[0-?]*[ -/]*[@-~]')
def strip_csi(text):
return CSI.sub('', text)
strip_csi('\x1b[31mError\x1b[0m') # 'Error'
This pattern does not remove other sequence types. Operating System Command sequences, which start with ESC] and end with BEL or ESC, are not covered, and neither are two-character escapes. If the input can contain those, use a fuller pattern or a dedicated library, and check the cleaned text for any remaining U+001B characters before trusting it.
Limits of this review
The descriptions above are based on the published definitions of the character and of escape sequences, and on the documented behavior of common tools. They are not based on tests of specific terminal emulators, chat apps, editors or screen readers, which can differ in how they display, copy or announce control characters. Before relying on a particular destination, paste a sample containing ESC into it and inspect the result with one of the methods above.