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

I expected this to output www.google.com in green letters.

Instead I got

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

5
  • Is your shell really bash? I suspect it's not. If this is a script, put #!/bin/bash at the top. Commented May 23, 2017 at 23:05
  • @JohnKugelman Yes, the shebang is already at the top.
    – user299709
    Commented May 23, 2017 at 23:07
  • 1
    How are you running it? ./script.sh or sh script.sh? Don't do the latter. Commented May 23, 2017 at 23:17
  • the latter~!!!!!
    – user299709
    Commented May 23, 2017 at 23:17
  • Its working fine on my Ubuntu, Kali, Backtract and Parrot also. Could you tell me which version of Linux you are working with? Commented May 23, 2017 at 23:28

2 Answers 2

5

Depending the environment or shell used can have an effect, one thing you could probably do is to use ANSI-C quoting:

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

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

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

1
  • 1
    @user299709: One other alternative is to use the escape code \033 instead of \e (eg. echo -e '\033[1;34m'"${domain}"'\033[0m')... some shells don't use \e and in such a case you'll generally have to use an alternative means of escaping characters.
    – l'L'l
    Commented May 23, 2017 at 23:32
3

If you run a script with sh script.sh, you're explicitly using sh as the shell rather than the one in the shebang line. That's bad news if sh isn't a link to bash. A plain sh shell may not support echo -e.

Type ./script.sh to use the interpreter in the shebang line.

3
  • while this is a valid answer, I chose ILI's answer as it directly allowed me to display colors without having to change the way I was running the script.
    – user299709
    Commented May 23, 2017 at 23:25
  • @user299709 Don't run a script with sh if it uses bash-specific features.
    – chepner
    Commented May 24, 2017 at 0:57
  • You should change the way you're running the script, though. Commented May 24, 2017 at 1:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.