Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do console colors in bash script

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:

enter image description here

Instead, I see this in the console output:

enter image description here

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 bash
  • echo -e
  • Standard ANSI escape sequences
  • A normal-looking terminal output

But no color.

Note:

  • The script is executed in non-interactive contexts

  • Sometimes locally

  • Sometimes through:

    • CI-style runners
    • Remote SSH execution
    • Job logs rendered in a web UI
  • The output is not always a direct interactive terminal (TTY)

However:

  • The output does preserve other formatting (newlines, spacing, etc.)
  • It is not obvious to me whether ANSI is being stripped, escaped, or just ignored

I have already tried:

  • Using echo -e
  • Using echo $'\033[0;36mtext\033[0m'
  • Exporting variables
  • Running the script directly vs sourcing it
  • Verifying $TERM (it is usually xterm-256color)
  • Verifying the shell is actually Bash
  • Making sure I am not in sh
  • Swearing at my computer

Same 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):

  1. echo is unreliable and should not be used for ANSI output
  2. The output stream is not a TTY, so ANSI is not interpreted
  3. The environment capturing stdout is escaping ANSI
  4. The terminal emulator / log renderer does not support ANSI
  5. I am misunderstanding how ANSI color actually works in Bash

Why 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:

  • When ANSI colors cannot work
  • How to detect that case
  • Whether printf is required instead of echo
  • Whether I should just give up and remove color entirely

I am not looking for “works on my machine” answers. I want to understand why this fails and what the correct approach is.

like image 778
Flavio Avatar asked Aug 13 '26 19:08

Flavio


2 Answers

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?

like image 105
Ben Szekely Avatar answered Aug 16 '26 23:08

Ben Szekely


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
like image 30
Diego Torres Milano Avatar answered Aug 16 '26 22:08

Diego Torres Milano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!