Colorful output brings visual clarity and aesthetic appeal to the command line. By leveraging colorization techniques, developers and system administrators can easily differentiate between different types of information. This article explores the fundamentals to color output using bash script, including syntax, formatting options, and color codes. Discover how to enhance your command-line workflow and navigate with ease.
Key Takeaways
- Colorizing the Bash output.
- Configuring the text style of the bash output.
Basic Syntax for Colorizing
The basic syntax for colorizing the output is as follows:
\033[FORMAT;COLOR CODEmYour Text\033[0m
Some common formatting and color codes are listed below for your ease.
A. Formatting options
Code
Description
0
Resets all formatting options.
1
Makes the text bold.
2
Makes the text appear dim.
4
Underlines the text.
5
Makes the text blink.
7
Reverses the foreground and background colors.
8
Hides the text (useful for password prompts).
B. Foreground color codes
Code
Description
30
Black
31
Red
32
Green
33
Yellow
34
Blue
35
Magenta
36
Cyan
37
White
90 to 97
Set brighter versions of the foreground colors.
C. Background color codes
Code
Description
40
Black
41
Red
42
Green
43
Yellow
44
Blue
45
Magenta
46
Cyan
47
White
100 to 107
Set brighter versions of the background colors.
Free Downloads
Step-by-Step Process to Change the Color Of Output Using Bash Script
You can develop Bash scripts that can change the color of the output of the Bash. Here I will develop a bash script that will print some lines on the terminal with different font colors and background colors. To do so, follow the below procedures.
Steps to Follow >
❶ At first, launch an Ubuntu terminal.
❷ Write the following command to open a file in nano:
nano color.sh
❸ Copy the script mentioned below:
!/bin/bash
green='\033[0;32m' #defining green variable
blue='\033[0;34m' #defining blue variable
echo -e "${green}Hello ${blue}How are you?" #prints a line with green and blue color
echo -e "\033[0;43mThis text has a yellow background\033[0m" #prints a line in yellow background
❹ Then, press CTRL+S to save the file and CTRL+X to exit.
❺ Execute the following command to run the bash script.
bash color.sh
In the above image, you can see that the bash script has printed a few lines with different text colors and background colors.
Miscellaneous Application: Using Bash Script to Print Output in Multiple Colors and Styles
You can style your lines as you wish. Here I will develop a script that will print lines of different styles with different colors To do so, you can use the following script.
Script (linesty.sh) >
#!/bin/bash
echo -e "\033[1;31mThis text is bold and red\033[0m" #This text will be bold and red
echo -e "\033[4;32mThis text is underlined and green\033[0m" #This text will be underlined and green
Now, run the script by executing the following command:
bash linesty.sh
In the above image, you can see that the bash script has printed a line with the bold format and red color another line that is underlined and green color.
Conclusion
In conclusion, changing the color of output using a Bash script adds readability and visual appeal to command line programs. By incorporating ANSI escape sequence and color codes, developers can customize the out to enhance user experience. Mastering this skill allows for more visually appealing scripts and improved information conveyance.
People Also Ask
Related Articles
- How to Print Output in Bash [With 6 Practical Examples]
- What is Echo Command in Bash [With 3 Practical Examples]
- How to Echo New Line in Bash [6 Practical Cases]
- Cat Command in Bash [With 4 Practical Examples]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Suppress Output in Bash [3 Cases With Examples]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial