MacOS ships an ancient version of bash (3.2.x), so bash scripts that work on other systems might not work on macOS. The version of bash on macOS does not support \e
as an escape sequence in echo
.
This appears to be a bug in bash 1.x through 3.x, fixed in 4.0. The manual lists \e
as one of the escape sequences supported by echo
, but help echo
lists \E
and not \e
. (Though digging into the code I can't see where \e
and \E
are treated differently — but experimentally, they are.)
(There's definitely a bug because of the inconsistency. But I think back then supporting only \E
was the desired behavior, presumably because it was a nonstandard extension and there's a habit in shell circles, mostly coming from command line options, that lowercase letters are standard things and uppercase letters are nonstandard things or modern additions. So the bug was that the manual wrongly listed \e
.)
printf
and $'…'
use different code for backslash escapes, and both recognize \e
even in bash 3.2.x. Alternatively, you can use an octal escape sequence, which works in all sh-style shells.
So any of the following will work instead of echo -e -n '\e…'
:
echo -e -n '\033…'
(works in most sh-style shells, but some don't support -e
or -n
as an option)
printf '\033…'
(note that any %
must be doubled; use %s
and an extra argument if there's a variable) (works in all sh-style shells)
printf %b '\033…'
(works in all sh-style shells)
printf '\e…'
(note that any %
must be doubled; use %s
and an extra argument if there's a variable)
printf %b '\e…'
echo -n $'\e…'
echo -n -e '\E…'
ls
cares whether you run it from Bash, Zsh, or Python).tput
instead of bare escape codes.) What is the output ofecho "$TERM"
?printf
instead ofecho -e
worked. just that i do not understand the problem...? echo $TERM is xterm-256colorecho -e
is nonstandard, but the symptom suggests that you are running the script incorrectly. Running it withzsh
or.
will bypass the shebang and force-feed it to Zsh; don't do that.