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.Answer1:
Short answer: <strong>It depends on the host string parser.</strong><hr />
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>.
\x1b(character 0x1B) should work.</li> <li>For parsers that support octal escapes (
\033(octal 33) should work.</li> <li>For parsers that support unicode escapes (
\u001Bshould work.</li> </ul><blockquote>
\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.
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
> 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