In Linux, the echo command is used to display text messages in the terminal, but did you know that you can spice up your terminal output by changing the color of the text?
It’s not just about black and white anymore! With a few simple tricks, you can add a splash of color to your terminal messages, making them more eye-catching and easier to read.
Understanding Color Codes in Linux
Before we dive into changing the output color of ‘echo‘ let’s understand how color codes work in Linux terminals.
In Linux, colors are represented by special escape sequences that the terminal interprets and displays accordingly. These escape sequences start with the \e[
characters, followed by parameters specifying the text and background colors.
Each parameter is separated by a semicolon (;)
and the 'm'
stands for “modifier“, which is used to mark the end of the color formatting sequence.
Here’s a breakdown of the color parameters:
- Text Color: Denoted by a number ranging from 30 to 37.
- Background Color: Denoted by a number ranging from 40 to 47.
For example, the following color codes sets the text color to red (31) and the background color to yellow (43).
\e[31;43m
Linux Color Code Table
Here’s a handy table showcasing some common text and background color codes:
Text Color | Background Color |
---|---|
30 – Black | 40 – Black |
31 – Red | 41 – Red |
32 – Green | 42 – Green |
33 – Yellow | 43 – Yellow |
34 – Blue | 44 – Blue |
35 – Purple | 45 – Purple |
36 – Cyan | 46 – Cyan |
37 – White | 47 – White |
Changing Output Color with Echo in Linux
Now, let’s see how you can use these color codes with the ‘echo‘ command to change the text color in the terminal.
To change the output color, you need to prepend the ‘echo‘ command with the desired color code as shown:
echo -e "\e[<text_color_code>;<background_color_code>mYour Message"
Here’s a real example using the echo command in a terminal:
echo -e "\e[31;43mWelcome to UbuntuMint"
Examples of Text Color Formatting in Terminal
Let’s illustrate this with a few more examples using different color combinations:
1. Displaying text in red with a green background.
echo -e "\e[31;42mHello, World!"
2. Displaying text in blue with a white background.
echo -e "\e[34;47mWelcome to the terminal!"
3. Displaying text in yellow with a cyan background.
echo -e "\e[33;46mThis is a colorful message!"
4. Displaying bold red text on a yellow background.
echo -e "\e[1;31;43mError: File not found!"
5. Highlighting important information with underlined cyan text on a black background.
echo -e "\e[4;36;40mIMPORTANT: Please remember to save your work!"
6. Drawing attention with blinking white text on a magenta background.
echo -e "\e[5;37;45mALERT: System overheating detected!"
7. Informing users with italicized green text on a blue background.
echo -e "\e[3;32;44mNote: Remember to update your system regularly."
8. Making warnings stand out with strikethrough yellow text on a red background.
echo -e "\e[9;33;41mWARNING: This action cannot be undone!"
These examples demonstrate how you can combine different attributes such as bold, underline, blink, italic, and strikethrough with various text and background colors to create visually appealing and informative terminal messages in Linux.
Conclusion
By using color codes with the ‘echo‘ command in Linux, you can customize the appearance of your terminal output.
Whether you’re highlighting important messages or just adding some flair to your scripts, changing text colors can make your terminal experience more engaging and visually appealing.
So, go ahead, experiment with different color combinations, and make your Linux terminal truly yours!