How to get osx shell script to show colors in echo
Solution 1:
Use \033
or \x1B
instead of \e
to represent de <Esc>
character.
echo -e "\033[1;31m This is red text \033[0m"
See http://misc.flogisoft.com/bash/tip_colors_and_formatting
Solution 2:
OSX ships with an old version of Bash that does not support the \e
escape character. Use \x1B
or update Bash (brew install bash
).
Even better, though, would be to use tput
.
Solution 3:
In script files printf
could be yet another option, you have to add trailing "\n"
though.
#!/bin/bash
echo -e "\e[31mOutput as is.\e[m"
printf "\e[32mThis is green line.\e[m\n"
printf "\e[33;1m%s\n" 'This is yellow bold line.'
Tested on macOS High Sierra 10.13.6:
% /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
Solution 4:
Another option could be using zsh, which respects the \e
notation.
#!/bin/zsh