0

I'm trying to add colors to a bash script I'm writing, and I'm following this tutorial.

Wehenver I copy/paste and run in my OSX terminal (OSX 11.3.1)

#! /usr/bin/env bash

RED="\e[31m"
GREEN="\e[32m"
ENDCOLOR="\e[0m"

echo -e "${RED}This is some red text, ${ENDCOLOR}"
echo -e "${GREEN}And this is some green text${ENDCOLOR}"

I get the proper output, with the colored text. However, after saving this in a file, colors.command and run chmod +x colors/command and then try to run the script either by double clicking it, or ./colors.command the output I get in my terminal is:

\e[31mThis is some red text, \e[0m
\e[32mAnd this is some green text\e[0m

How can I fix this so that my script outputs the colors properly when run?

Lucas P.
  • 4,282
  • 4
  • 29
  • 50
  • 1
    As stated in [one of these comments](https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux#comment24883926_5947802), for MacOs, use `\033` instead off `\e`. So for red, use `RED="\033[31m"` – 0stone0 May 31 '21 at 11:33
  • 1
    Replacing `\e` with `\033` solved my issue, thank you so much! – Lucas P. May 31 '21 at 11:43
  • @LucasP. : I would not expect double-clicking a shellscript file to magically cause colours to appear, but I created a file with the exact content of your program, and when I run it, I can see the coloured output. – user1934428 May 31 '21 at 11:44
  • @user1934428 what OS are you using? On MacOS it only works after following 0stone0's suggestion – Lucas P. May 31 '21 at 12:25
  • 1
    @LucasP. `cygwin` running on Windows 10 inside a mintty terminal, and `TERM` set to _xterm-256color_. – user1934428 May 31 '21 at 12:27

0 Answers0