Color-coded output enhances the readability of Bash scripts. It helps identify important messages, warnings, and errors more clearly. Terminal emulators interpret ANSI escape sequences to produce these colors, but compatibility should be checked on different environments.
In most cases, terminals that support ANSI color sequences will render colored text when these escape codes are used. Each code matches a specific text color, background color, or display setting. Many programming tasks rely on these codes to highlight warnings, notices, or status information.
When implementing color codes, it is important to handle potential variations across shells and terminal types. Some scripts rely on tput, while others embed escape sequences directly in their echo commands. Both approaches allow precise control of foreground and background colors, along with style attributes.
Steps to enable color using echo with ANSI codes:
Colorizing output with echo involves adding special escape sequences before and after text. These sequences often start with “\e[” or “\033[” and specify a color or formatting code.
- Define variables for each color code to keep the script organized.
- Insert a reset code at the end of each colored message to restore default formatting.
- Use backslash-escaped sequences in echo commands to produce colored text.
$ echo -e "\e[31mThis is red text\e[0m" This is red text
\e[31m sets the text color to red, and \e[0m resets the color.
- Store frequently used colors in variables and reference them in the script.
Predefined color variables reduce duplication and make updates easier.
- Verify terminal support by checking the value of TERM.
Some terminals may not render ANSI escape codes properly.
Steps to enable color with tput:
Using tput provides a more portable way of handling color codes. It queries the terminal’s capability database and adjusts the escape sequences accordingly. This can help avoid issues on systems that do not use standard ANSI color codes.
- Confirm the installed ncurses library, which enables tput commands to interact with the terminal.
- Retrieve color codes using tput to set foreground and background colors.
$ tput setaf 2 $ echo "This text is green" This text is green
- Store output from tput in variables for reuse in scripts.
Example: GREEN=“$(tput setaf 2)” then echo “${GREEN}Text${RESET}” for color formatting.
- Reset formatting with tput sgr0 to restore default text settings.
- Confirm terminal capabilities with infocmp for full compatibility details.
Check if your terminal supports the required color modes or fallback to basic modes.

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.