Saturday, 14 March 2026

Color coding in bash

Bash Script to Display All 256 ANSI Terminal Colors 🎨

Many modern terminals support the ANSI 256-color palette, allowing scripts and command-line tools to display colored text.

Using colors in terminal output makes scripts easier to read and is commonly used for:

  • Shell scripts

  • CLI tools

  • Log highlighting

  • Prompt customization

  • Monitoring scripts

The following Bash script prints all 256 terminal colors (0–255) so you can visually inspect how each color appears in your terminal.


1️⃣ The Bash Script

#!/usr/bin/env bash

for i in {0..255}; do
printf '\e[38;5;%dmColor %d\e[0m\n' "$i" "$i"
done

This simple loop prints the text “Color N” using each of the 256 ANSI colors.

























2️⃣ Understanding the Loop {0..255}

for i in {0..255}

This syntax is called Bash brace expansion.

It generates the numbers:

0 1 2 3 ... 255

So the loop runs 256 times, storing each value in the variable i.

Example iterations:

i=0
i=1
i=2
...
i=255

Each value corresponds to a specific ANSI color index.


3️⃣ The printf Command

printf '\e[38;5;%dmColor %d\e[0m\n' "$i" "$i"

printf prints formatted text to the terminal.

It works similarly to the C programming language printf() function.

In this script, it performs two tasks:

  1. Applies a color escape sequence

  2. Prints the color number


4️⃣ Understanding the ANSI Color Escape Code

\e[38;5;%dm

This is an ANSI escape sequence that tells the terminal to change the text color.

PartMeaning
\eEscape character
[Start of ANSI control sequence
38Set foreground text color
5Use 256-color mode
%dColor number (0–255)
mApply formatting

Example

When i=34 the sequence becomes:

\e[38;5;34m

This tells the terminal:

👉 Set the foreground text color to color 34


5️⃣ The Printed Text

Color %d

%d is replaced with the current value of i.

Example output:

Color 34

But the text “Color 34” appears in color 34.


6️⃣ Resetting Terminal Formatting

\e[0m

This sequence resets all terminal formatting.

Without resetting:

  • The terminal would keep using the same color

  • All text printed afterwards would remain colored

Resetting ensures the terminal returns to default colors.


7️⃣ Example Output

When the script runs, you will see:

Color 0
Color 1
Color 2
...
Color 255

Each line appears in its corresponding color, creating a visual color reference chart directly inside your terminal.


8️⃣ Understanding the ANSI 256-Color Palette

The 256-color palette is organized into three sections:

RangeDescription
0–15Standard system colors
16–2316×6×6 RGB color cube
232–255Grayscale ramp

This means the terminal supports:

  • Basic colors

  • Extended RGB-style colors

  • Grayscale shades


9️⃣ Using True RGB Colors (24-bit Color)

Modern terminals also support True Color (24-bit color).

Instead of:

38;5;<color>

You can use:

38;2;<R>;<G>;<B>m

Structure:

\e[38;2;<R>;<G>;<B>m

Where:

ValueMeaning
RRed intensity (0–255)
GGreen intensity (0–255)
BBlue intensity (0–255)

🔴 Example: Pure Red Text

printf "\e[38;2;255;0;0mHello\e[0m\n"

Meaning:

ValueMeaning
255Red
0Green
0Blue

Output:

Hello

Displayed in bright red.


🔟 Additional ANSI Styling Options

ANSI codes can also control text styles.

CodeEffect
1Bold
4Underline
7Reverse
0Reset

Example

printf "\e[1;38;5;196mBold Red Text\e[0m\n"

This prints bold bright red text.


1️⃣1️⃣ Background Colors

To change the background color, use 48 instead of 38.

Example:

printf "\e[48;5;34mBackground Color\e[0m\n"

This prints text with background color 34.


1️⃣2️⃣ Checking Terminal Color Support

You can check whether your terminal supports 256 colors.

Method 1

echo $TERM

Method 2

tput colors

Example output:

256

This means your terminal supports the 256-color palette.


1️⃣3️⃣ Using tput for Portable Color Scripts

Instead of raw escape sequences, you can also use the tput command, which interacts with the terminfo database.

This approach is more portable across terminals.

Example script:

[root@oel01db Shell-Scripting]# cat 02-color-text-using-tput.sh

#!/usr/bin/env bash

# Define colors

RED=$(tput setaf 1)

GREEN=$(tput setaf 2)

YELLOW=$(tput setaf 3)

RESET=$(tput sgr0)

# Use them in your script

echo "${RED}Error:${RESET} Something went wrong."

echo "${GREEN}Success:${RESET} File moved."

echo "${YELLOW}Warning:${RESET} Disk space is low."

[root@oel01db Shell-Scripting]#












1️⃣4️⃣ Why tput Is Often Preferred

tput is useful because:

  • It abstracts terminal capabilities

  • It works across different terminal types

  • It relies on the terminfo database

Common commands:

CommandMeaning
tput setaf NSet foreground color
tput setab NSet background color
tput boldBold text
tput sgr0Reset formatting

1️⃣5️⃣ Practical Use Cases

Colorized terminal output is extremely useful for:

1. Log highlighting

ERROR (red)
WARNING (yellow)
SUCCESS (green)

2. Deployment scripts

Clear feedback during automation.

3. Monitoring scripts

Color helps quickly identify issues.

4. Custom Bash prompts

Many developers use colors in the PS1 prompt.


Conclusion

The ANSI color system allows Bash scripts to produce rich, readable terminal output.

Using either ANSI escape sequences or the tput command, you can easily:

  • Colorize logs

  • Improve CLI tools

  • Create visually helpful scripts

  • Customize your shell environment

The 256-color reference script is a simple yet powerful tool for understanding how colors appear in your terminal.

No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...