On mac OSX, I have this script:
#!/usr/local/bin/bash
echo -e "\e[41mError: Some error.\e[0m"
When I just run the echo -e ... in a console, it prints the colored text "Error: Some error."
When executed as a script sh myscript.sh, it litterally prints the flag and the escape characters: -e "\e[41mError: Some error.\e[0m".
When I add the script location to ~/.bash_profile and execute it as myscript.sh, it does work. But I need to be able execute it without adding it to my bash profile.
Edit: using printf works: printf "\e[41mError: Some error.\e[0m\n".
/usr/local/bin/bashexists?shif you want to run with with bash#!/bin/bashas shebang, then useecho -e '\033[0;31mError: Some Error.\033[0m'and call it with./script.shtest -t 2for stderr. I like to implement this by using variables for the control sequences (likered_2='\e[41m'), and then only set them if the test returns true for that fd.echobehave differently when parsing options and/or escape sequences; I had a bunch of my scripts break when Mac OS X 10.5 (I think that was the version) came out and its bash built-in changed behavior(!).printfis more complex to use properly (its first argument is a format string that's used to control how the rest of the arguments are printed), but it's way more predictable. For this, I'd useprintf '\e[41m%s\e[0m' "Error: Some error.". See this and this.