Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get osx shell script to show colors in echo

People also ask

How do I enable colors in Terminal Mac?

Use Text preferences in Terminal to change the font, text, colour and cursor options for a Terminal window profile. To change these preferences in the Terminal app on your Mac, choose Terminal > Preferences, click Profiles, select a profile, then click Text.


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


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.


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.

Another option could be using zsh, which respects the \e notation.

#!/bin/zsh