I have a Bash script that prints status headers using ANSI color escape codes. The script runs, the output prints, but instead of seeing colored text, I see the literal escape sequences printed to the console, for example [0;36m instead of cyan text.
I cannot tell whether the issue is Bash, echo, the shell, the terminal, or the execution environment.
I expect pretty colors in the console:

Instead, I see this in the console output:

So clearly the escape codes are being printed, not interpreted.
Relevant code:
CYAN="\033[0;36m"
NC="\033[0m"
echo -e "${CYAN}==============================${NC}"
echo -e "${CYAN}Terminus Task Difficulty Calibration${NC}"
echo -e "${CYAN}==============================${NC}"
I am explicitly using:
#!/usr/bin/env bashecho -eBut no color.
Note:
The script is executed in non-interactive contexts
Sometimes locally
Sometimes through:
The output is not always a direct interactive terminal (TTY)
However:
I have already tried:
echo -eecho $'\033[0;36mtext\033[0m'$TERM (it is usually xterm-256color)shSame result.
If I run other scripts that use ANSI color, they sometimes work.
If I paste ANSI escapes directly into the terminal, they sometimes work.
But this script, when executed as part of a job or log output, prints raw escape codes.
So my working theory is one of the following (but I want confirmation):
echo is unreliable and should not be used for ANSI outputWhy is my Bash script printing raw ANSI escape codes instead of colored output, and what is the correct, portable way to print colored text in Bash that works across real-world environments (including CI / SSH / job logs)?
I'd also like to know:
printf is required instead of echoI am not looking for “works on my machine” answers. I want to understand why this fails and what the correct approach is.
I think your terminal emulator can't work with ANSI escape codes.
Have you tried running it in a terminal instead of the (I assume) Git checker you're using?
Do something like this, to avoid sending the escape sequences when not a tty
#! /bin/bash
cyan=$(tput setaf 6)
sgr0=$(tput sgr0)
if tty -s; then
printf '%swith color%s\n' "$cyan" "$sgr0"
else
printf 'no color\n'
fi
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