I have a code like this:
#!/bin/bash
COLOR_REST='\e[0m'
COLOR_GREEN='\e[0;32m'
echo -e "${COLOR_GREEN}OK${COLOR_REST}"
When I copy and paste the code into my iTerm, it displays OK in the color of green.

However, when I store the code in a file named testColor.sh, and execute ./testColor.sh. It displays \e[0;32mOK\e[0m on my screen.
Why doesn't it display OK with green?
I've also tried bash testColor.sh, and sh testColor.sh. Both fail to display the text in green.
Another thing I feel strange is that I don't see the -e option in the BSD General Commands Manual in man echo.
I'm using macOS High Sierra as my operating system.
Use
#!/bin/bash
COLOR_REST="$(tput sgr0)"
COLOR_GREEN="$(tput setaf 2)"
printf '%s%s%s\n' $COLOR_GREEN 'OK' $COLOR_REST
which uses printf to avoid echo options and tput to be portable across different terminals.
Using printf instead of echo should work in any POSIX compliant shell. I tried this in High Sierra with the default terminal and it didn't work (there weren't any extended options, just -n).
As for an explanation, I haven't used iTerm so I'm not completely sure, but could be a different echo implementation by iTerm, which would cause the flag to work only when using iTerm itself, not /bin/bash. Notice that in the man page for echo, it says
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With