Best Ansi Escape beginning


Which Ansi escape sequence is the most portable and/or simply best and why?

1. "\u001B[32;1mThis is bright green\u001B[0m" 2. "\x1B[33;1mThis is bright yellow\x1B[0m" 3. "\e[35;4;1mThis is bright purple underlined\e[0m"

I have been using printf "\x1B[32;1mgreen\x1B[0m" (that's an example in unix bash script for example) out of habit, but I was wondering if there were any reasons to use one over the other. Is one more portable than the others? That would be my assumption.

Also, if you know of any other Ansi Escape sequence feel free to share it in the comments or at the end of your answer.

If you don't know what an Ansi Escape sequence is or want to become more familiar with it, then here you go: <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow">http://en.wikipedia.org/wiki/ANSI_escape_code</a>


All of the escape sequences above have worked on all of the Unix systems I have been on, however one must still rely on the system itself to interpret the escape codes. Windows, for example, does <strong>not</strong> permit any sort of escape codes except four (BEL, L-F or linefeed, C-R or carriage return and, of course, BS or backspace), so Ansi escape sequences will not work.


Short answer: <strong>It depends on the host string parser.</strong>

<hr />

Long answer:

It depends on the string parser; that is, the piece of code that actually takes in your string ("\x1b[1mSome string\x1b[0m") as a literal and parses the <a href="http://en.wikipedia.org/wiki/Escape_character" rel="nofollow">escape characters</a> using the backslash <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow">ANSI escape sequence</a>.

<ul><li>For parsers that support hexadecimal escapes (\x), then \x1b (character 0x1B) should work.</li> <li>For parsers that support octal escapes (\ddd), then \033 (octal 33) should work.</li> <li>For parsers that support unicode escapes (\u), then \u001B should work.</li> </ul><blockquote>

Quick elaboration: \x and \u are similar; \x usually refers to a single character, 0-255, in <strong>h</strong>exadecimal radix. \u means the same (as it is represented in hexadecimal), but supports two bytes (in most parsers) and generally refers to 16-bit <strong>u</strong>nicode characters.

</blockquote> <hr />

A lesser used/supported escape character, as you mentioned, is \e. This escape is most commonly used with parsers/languages that expect a lot of ANSI escaping to happen, such as bash (and most other shells).

For instance, <a href="https://nodejs.org/" rel="nofollow">Node.js</a> does not support \e:

> console.log("\x1b[31mhello\x1b[0m") hello undefined > console.log("\e[31mhello\e[0m") e[31mhelloe[0m undefined

Neither does <a href="http://www.lua.org/" rel="nofollow">Lua</a>:

<pre class="lang-lua prettyprint-override">> print('\x1b[31mhello\x1b[0m') hello > print('\e[31mhello\e[0m') stdin:1: invalid escape sequence near '\e'

Or even <a href="https://www.python.org/" rel="nofollow">Python</a>:

<pre class="lang-py prettyprint-override">>>> print("\x1b[31mhello\x1b[0m") hello >>> print("\e[31mhello\e[0m") \e[31mhello\e[0m >>>

Though <a href="http://php.net/" rel="nofollow">PHP</a> does:

<pre class="lang-php prettyprint-override"><?php echo "\x1b[31mhello\x1b[0m\n"; // hello echo "\e[31mhello\e[0m\n"; // hello


  • MVC5 Ajax.BeginForm upload form with files
  • How to find the Custom Field detail of a product backlog item TFS by WIQL
  • “^” in regex pattern make an error
  • Sonata Admin Bundle custom group icon
  • Change font size to fit text inside button entirely?
  • Rectangular Nesting - Convergence to optimal solution using Simulated Annealing
  • How can I compose a VM into a view within an Aurelia validation renderer
  • Toggle DIV state localstorage
  • Javascript onclick does not fire when developer tools are open
  • Create unique ids for a group
  • Ordering of eigenvectors when calculating eigenvectors using LAPACK's ssteqr
  • Finding regular expressions for languages otherwise described
  • Can I disable IE compatibility mode only for content within a ?
  • How would I send and receive packets over a WebSocket in Javascript
  • Reflection / C# typing errors when publishing an F# class implementing an interface
  • SQL Server 2008 R2 - Islands and Gaps [closed]
  • Validity Method for Reference Classes
  • JSON Error when parsing “… has no method 'replace'”
  • How can I count unique terms in a plaintext file case-insensitively?
  • Does the Azure table storage API cache results?
  • TSQL Rolling Average of Time Groupings
  • Oracle - Second level subquery cannot see field from main query
  • Synchronize windows folders
  • Easiest way to get current unix timestamp via XSL
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Overlapping controls in Windows XP
  • Implicit joins and Where in Doctrine - how?
  • Create Instant using a negative year
  • Button text different than value submitted in query string
  • Ensure fsync did its job
  • How do I open a C file with a relative path?
  • Java applet as stand-alone Windows application?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • log4net write single file for each call to log.info
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy