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:
-
Applies a color escape sequence
-
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.
| Part | Meaning |
|---|---|
\e | Escape character |
[ | Start of ANSI control sequence |
38 | Set foreground text color |
5 | Use 256-color mode |
%d | Color number (0–255) |
m | Apply 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:
| Range | Description |
|---|---|
| 0–15 | Standard system colors |
| 16–231 | 6×6×6 RGB color cube |
| 232–255 | Grayscale 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:
| Value | Meaning |
|---|---|
| R | Red intensity (0–255) |
| G | Green intensity (0–255) |
| B | Blue intensity (0–255) |
🔴 Example: Pure Red Text
printf "\e[38;2;255;0;0mHello\e[0m\n"
Meaning:
| Value | Meaning |
|---|---|
| 255 | Red |
| 0 | Green |
| 0 | Blue |
Output:
Hello
Displayed in bright red.
🔟 Additional ANSI Styling Options
ANSI codes can also control text styles.
| Code | Effect |
|---|---|
| 1 | Bold |
| 4 | Underline |
| 7 | Reverse |
| 0 | Reset |
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:
| Command | Meaning |
|---|---|
tput setaf N | Set foreground color |
tput setab N | Set background color |
tput bold | Bold text |
tput sgr0 | Reset 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