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

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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.

Syntax for Colorizing Text in Bash

To color the output in Bash, follow the below syntax:

\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.

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).

Bash Color Codes

Foreground Code Background Code Color
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Magenta
36 46 Cyan
37 47 White
90 to 97 100 to 107 Set brighter versions of the colors.

Steps 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:

  1. At first, launch an Ubuntu terminal.
  2. Write the following command to open a file in nano:
    nano color.sh
  3. 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.
  4. Then, press CTRL+S to save the file and CTRL+X to exit.
  5. 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.

How to Print Output in Multiple Colors in Bash?

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.

#!/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
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