How to Change Color of Output Using Bash Script? [Easy Guide]

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

EXPLANATION
Here, FORMAT represents the formatting options, and COLOR CODE represents the color to apply. The escape sequence \033[0m is used to reset the formatting to the default.

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

EXPLANATION
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then the green=’\033[0;32m’ and the blue=’\033[0;34m’ commands will define two variables named green and blue. Afterward, the echo -e “${green}Hello ${blue}How are you?” command will print the “Hello” text with green color and the “How are you?” text in blue color. Then, echo -e “\033[0;43mThis text has a yellow background\033[0m” command prints a line in yellow background. The -e flag in both lines with echo enables the interpretation of escape sequences for colorizing.

❹ 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

EXPLANATION
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then, the echo -e “\033[1;31mThis text is bold and red\033[0m” command will print a bold and red colored line. And, the echo -e “\033[4;32mThis text is underlined and green\033[0m” command will print an underlined and green colored line. The -e flag is used with echo to enable the interpretation of escape sequences for colorizing.

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

How many colors does bash support?
Some terminals can support 88 colors and some terminals can support up to 256 colors. To ensure check the compatibility list of your system.

How do I use color in bash?
Add a \e at the beginning to form an escape sequence. The escape sequence for specifying color codes is \e[COLORm (COLOR represents our color code in this case). By default, echo does not support escape sequences. You need to add the -e option to enable their interpretation.

What is the color scheme in bash?
The bash environment does not have any color by default. Initially, everything is in black-and-white mode.

How do terminal colors work?
Mapping the 8 colors to RGB values is the role of terminal color schemes. Most terminals support an additional 8 colors corresponding to the bold or bright variants of the original colors.

Related Articles


<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment