How to Print Colored Text to the Linux Terminal
Last Updated :
13 Sep, 2024
Printing colored text to the Linux terminal can greatly enhance the visual appeal and readability of your command-line applications or scripts. While the terminal traditionally displays text in monochrome, incorporating colors can provide crucial context, highlight important information, or simply add a touch of aesthetic appeal.
Here, we will explore various methods to print colored text to the Linux terminal, covering everything from basic ANSI escape sequences to more advanced techniques using libraries like 'tput' and 'terminfo'.
Methods to Print Colored Text to the Linux Terminal
Here are some of the most common and effective methods to print colored text in the Linux terminal:
1. Using ANSI Escape Sequences
ANSI escape sequences are a standard for controlling text formatting and colors in terminal environments. They consist of special character sequences preceded by the escape character (\e or \033). Here’s how you can use ANSI escape sequences to print colored text:
echo -e "\e[31mThis text is red\e[0m"
changing text to redIn this example:
- \e[31m sets the text color to red.
- \e[0m resets the text formatting to the default.
Common ANSI Color Codes:
ANSI Code | Color |
---|
31 | Red |
32 | Green |
33 | Yellow |
34 | Blue |
35 | Magenta |
36 | Cyan |
2. Using the 'tput' Command
The tput command is a POSIX standard utility that enables terminal-related operations, including setting text attributes and colors. Here's how you can use tput to print colored text:
Create script using vim editor.
vim colorchangingtext
#!/bin/bash
RED=$(tput setaf 1) RESET=$(tput sgr0) echo "${RED}This text is red${RESET}"
Executing Script
bash colorchangingtext
using tputIn this example:
- 'tput setaf 1' sets the foreground color to red.
- 'tput sgr0' resets text formatting.
You can replace 1 with other color codes as per the tput documentation.
Color Codes for tput:
Code | Color |
---|
0 | Black |
1 | Red |
2 | Green |
3 | Yellow |
4 | Blue |
5 | Magenta |
6 | Cyan |
7 | Whit |
3. Using the Terminfo Database
The terminfo database contains information about the capabilities of various terminals, including color support. You can leverage this database to print colored text dynamically based on the terminal’s capabilities. Here's how you can do it:
We'll create a script using vim editor
vim colorchange
#!/bin/bash
if [ $(tput colors) -ge 8 ]; then
RED=$(tput setaf 1)
RESET=$(tput sgr0)
echo "${RED}This text is red${RESET}"
else
echo "Terminal does not support colors."
fi
Executing Script
bash colorchange
Terminfo DatabaseThis script checks if the terminal supports at least 8 colors before printing colored text. If it does, it sets the text color to red; otherwise, it prints a message indicating that the terminal does not support colors.
4. Using Shell Functions for Reusability
To simplify the process of printing colored text, you can define shell functions in your scripts. Here's an example of a shell function for printing colored text:
We'll create a script using vim editor
vim color
#!/bin/bash
print_color() {
local color=$1
local text=$2
echo -e "$(tput setaf $color)$text$(tput sgr0)"
}
# Usage
print_color 2 "This text is green"
Executing Script
bash color
Using shell functionThis function takes two arguments: the color code and the text to be printed. It then applies the specified color to the text using tput.
Conclusion
Printing colored text to the Linux terminal is a straightforward yet powerful way to enhance the visual presentation of your command-line applications or scripts. Whether you opt for ANSI escape sequences, tput, or dynamic color detection using the terminfo database, mastering these techniques will allow you to create more engaging and informative terminal experiences. Experiment with different colors and formatting options to discover the best approach for your specific needs.
Similar Reads
Formatted text in Linux Terminal using Python
This article demonstrates how to print formatted text in Linux terminal using Python programming language. Formatted text(also called styled text or rich text) as opposed to plain text, has styling information like: color (text color, background color), style (bold or italic), and some other special
4 min read
How to add colour to text Python?
There are multiple ways supported by python in which color can be added to text. This article discusses all with proper examples to help you understand better. Method 1: Using ANSI ESCAPE CODE ANSI escape sequence is a sequence of ASCII characters, the first two of which are the ASCII "Escape" chara
2 min read
How to Set Text Color in RGBA in Tailwind CSS ?
In this article, we will change the color of text to rgba(0, 0, 0, 0.54) which is an RGB color format with an added parameter alpha that tells the opacity of the color. In this article, we are going to learn how we can achieve the text color of rgba(0, 0, 0, 0.54) in Tailwind CSS and hence, change t
3 min read
How to add special characters to text to print in color in the console in JavaScript ?
The purpose of this article is to add special characters to text to print in color in the console in JavaScript. Approach: The ANSI escape codes help change/specify the color of the output in the console. The color of the output of the console can be changed by adding these escape codes just before
2 min read
How to Customize Linux Terminal Using powerlevel10k?
Sometimes we get bored with the normal Linux Terminal and feel like if we can customize the same, so we can use powerlevel10k for that. It is a theme for ZSH. It changes normal shell commands to colorful commands. To install powerlevel10k we need to install Oh My Zsh, both are open-source in GitHub.
2 min read
How to Make Linux Terminal look Awesome
In this article, we will install and configure some themes and plugins to tweak our Linux terminal for better productivity and a fancier look. These features include autocompletion, autosuggestion, command-line search, syntax highlighting, and better support for git and environment managers. Zsh The
3 min read
eDEX-UI - The Tron-Inspired Terminal Emulator for Linux
Linux users often deal with the terminal but the look and feel of the terminal may be boring. Everyone wants a terminal that looks like a sci-fi movie screen. Then eDEX-UI will end your wait. eDEX-UI is a Sci-Fi inspired terminal emulator and system monitor. It gives a futuristic theme to the termin
3 min read
How to Change the Output Color of Echo in Linux
When working with a computer, some users may encounter a situation where they need to output text in a different color. The colors we are familiar with by now will have been worked out for our particular machine and thus can be replaced easily if needed. Echo is a Linux command that can be used to c
9 min read
How to Customize Bash Colors and Content in Linux Terminal Prompt
If you are using the Linux operating system, that means you use the CLI most of the time. And do more work on the terminal. By default, most Linux Operating systems provide you the bash shell. Shell provides the interface between the user and kernel and executes commands. In this article, we are goi
9 min read
How To Change The Text Color Using Tkinter.Label
In Tkinter, we can customize the styling of elements such as labels to enhance the appearance of our GUI applications. One common customization is changing the text color of a Label widget. In this article, we will explore different approaches to changing the text color using tkinter.label in Python
2 min read