Home » Questions » Computers [ Ask a new question ]

Why doesn't echo support "\e" (escape) when using the -e argument in MacOSX

Why doesn't echo support "\e" (escape) when using the -e argument in MacOSX

When I try to print out some colored text using ANSI escape sequences via the built-in echo command, it seems that the \e escape sequence in the string I provide is interpreted literally instead of as the "escape" it's supposed to represent. This only happens in Snow Leopard -- the examples below work as intended in Leopard.

Asked by: Guest | Views: 316
Total answers/comments: 5
Guest [Entry]

"I cannot tell you why it does not support that argument (you may have to ask the programmers about that).
I only know that on my linux box, I get this:

$ /bin/echo --help
Usage: /bin/echo [SHORT-OPTION]... [STRING]...
or: /bin/echo LONG-OPTION
Echo the STRING(s) to standard output.

-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version output version information and exit

If -e is in effect, the following sequences are recognized:
*emphasized text*
\0NNN the character whose ASCII code is NNN (octal)
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab

NOTE: your shell may have its own version of echo, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.

Report echo bugs to bug-coreutils@gnudotorg
GNU coreutils home page: <http://www.gnudotorg/software/coreutils/>
General help using GNU software: <http://www.gnudotorg/gethelp/>
Report echo translation bugs to <http://translationprojectdotorg/team/>

this does not mention \e escapes
it says that it is /bin/echo from gnu coreutils. As apple changes the source of their unix-system components from time to time (e.g. move from zsh to bash), check if there was a change for /bin/echo between Leopard and Snow Leopard. If it is gnu, you can ask the people at gnudotorg why they choose not to include those sequences.

As for workarounds (thats more interesting):
Not using /bin/echo, but bash's builtin echo works on linux boxes. If they changed to a bash without builtin echo (or something even more obscure), you could also try this not widely known feature of your shell (works at least in bash and zsh):

$ echo $'\e[34m''COLORS'

This is the matching part of bash's manpage:

Words of the form $'string' are treated specially. The word expands to string, with
backslash-escaped characters replaced as specified by the ANSI C standard. Backslash
escape sequences, if present, are decoded as follows:
\a alert (bell)
\b backspace
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value nnn (one to three
digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or
two hex digits)
\cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

A double-quoted string preceded by a dollar sign ($) will cause the string to be trans‐
lated according to the current locale. If the current locale is C or POSIX, the dollar
sign is ignored. If the string is translated and replaced, the replacement is double-
quoted."
Guest [Entry]

Does \033 still work? If not, you can hopefully press Ctrl+V followed by the Escape key (if a mac has those keys) to create a real control character inside the command line (which does not work that well in scripts of course, depending on the editor)
Guest [Entry]

"They might try to conform to POSIX:
http://www.opengroupdotorg/onlinepubs/9699919799/

Part OPTIONS states among other things:

Implementations shall not support any options."
Guest [Entry]

"FYI, We're just about to add \e support to /bin/echo and /usr/bin/printf in coreutils.
Note the C standards do not specify \e, but gcc, perl, bash, ksh and tcsh support it"
Guest [Entry]

You might check if ,,escape non-ascii characters'' have been un-checked in the view options menu of the terminal.app.