How to use echo command to output color escape sequence

domain="www.google.com"
echo -e "\e[1;34m"$domain"\e[0m"

      

I expected this to output in www.google.com

green letters.

Instead, I got

-e \e[1;34mwww.google.com\e[0m

+3


source to share


2 answers


Depending on what environment or shell you are using might have an effect, one thing you can probably do is use ANSI-C

quoting:

echo $'\e[1;34m'${domain}$'\e[0m'

      



Words of the form $ 'string' are specially processed. The word is expanded to a string with replaced backslashes as specified in the ANSI C Standard.

https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html

+4


source


If you run the script with sh script.sh

, you are explicitly using sh

as a wrapper, not the one on the shebang line. This is bad news if sh

not a link to bash

. Normal shell sh

may not support echo -e

.



Enter ./script.sh

to use the interpreter on the shebang line.

+3


source







All Articles