Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Change the Output Color of Echo in Linux

Improve Article
Save Article
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article

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 change the output color of the text. Any text entered using echo is displayed in the new color before being output. The input method for this feature varies: bash, zsh, and python (as well as other shell extensions) will provide option -E which disables it.  There are several ways of changing foreground and background colors in Linux. Some examples showing how to use echo are included on this page.

Change the Output Color of Echo in Linux

To change the color of the output using echo, users must first know the syntax of the command. First, let’s look at a sample command using no arguments to show the default color. The input color is blue, so the output should be green. Generally, the color of the commands follows the terminal’s color combination. In the below example, the commands and the Output are displayed in white color.

Fig 1.1 Default color

ANSI Codes

Changing the Output color of Echo in Linux can be achieved using the respective ANSI escape codes. “00” means echo -n; This is used as an output to the console, but will not work for a specific channel or voice you want to send your data with. In this example, we use 00:1c, which indicates that an important message should be sent directly into our mail server’s outgoing queue. If it didn’t receive any response from us after 20 seconds due to much long delay then there may be something wrong here. The ANSI for various commonly used colors can be seen below.

Fig 1.2 ANSI Code for various colors

The code should be given in the format: R=’\033[0;31m’. This has to be prefixed by an argument called \033[, followed by the code for the color that needs to be specified. Here, 0;31 means Red color.

1. Single Color In the output

Changing the Color of the echo Command’s Output in Linux can be achieved with an additional command: echo -e # or, if you prefer, this line is often used as a placeholder for console output.

Let us try to print the output in green color.

echo -e "${GREEN} Green Colored Statement"

Based on this sample command, echo output the input color with green text. To change the color of the output, use one of the escape sequences \ green or $ green. The former changes from red to green. 

Fig 1.3 Green Color Output

2. Multi Color In the output

Now, let us try to print a statement in many colors.

Fig 1.4 Multi Color

For No Color, the escape character is ‘\033[0m’

While changing the output color is straightforward, it can be challenging to choose which color to change it to. For example, if you want green text on white background, you should use $ white instead of $white. On the other hand, if you want blue background on red text, you should instead use $ red. There are many ways to customize your echo output. Using echo allows users to easily modify their computer’s output without requiring additional software or settings tweaks. Changing the text’s color makes it easy for users with visual impairments; this is especially useful when making web pages accessible for those with blindness or low vision. 

Add color to the output of printf

We can also use the same to color the printf output

Fig 1.5 Printf Output

Change color with the help of the Tput command

The command tput is used to change the text on the screen. It can be used to change the foreground and background colors. In addition, you can use the tput command to change the line wrapping, cursor position, and even text attributes like bold and underline. Tput commands are very useful for making a computer’s display look pretty.

To change the text color, type: tput green and press Enter. The prompt will change to reflect the new color. You can also enter commands to change other colors such as blue, red, and yellow. To reverse a color-change command, type: tput blank followed by Enter. The prompt will revert to its original color. You can delete a color with: tput null. To reset all colors back to their default values, use tput reset.

Fig 1.6 All 255 colors using tput command – setaf for foreground

Fig 1.6 colors using tput command – setab for background

The command tput is used to change the text on the screen. setf and setb commands are used to format data, respectively (these functions can only be applied from within script). c will reset it with an empty string if no output was written or returned by its last argument.

Change the Foreground and Background Color in Bash

 The Bash shell is the default shell for all new Linux, UNIX, and Mac OS X systems. This makes it one of the most popular shells in use today. The Bash shell is also very powerful; it offers an extensive range of options and functionality. Including a graphical user interface, the Bash shell is one of the most versatile in existence.

The Bash shell’s capabilities are largely based on its accompanying libraries and environment variables. These allow the user to access various parts of the operating system- even user applications. In addition, the shell’s color options allow the user to easily change their shell’s colors. Essentially, changing your background and text colors makes your output easier to read.

 There are a lot of ways to do foreground and background colors in bash. One popular way is to use $TERM=’~/.bash_color’. You can also set your own custom variables for each screen, e $ TERMCAP=$HOME/.bashrc, or when there’s an event that triggers it like a cat command.

We saw a lot of ways to change colors. The below shows the way to change the background color of the terminal.

Fig 1.7 Terminal

Fig 1.8 Bg change

Using the Escape Sequence in Bash

The Escape Sequence in Bash can also be used to color text in bash. “Bash: colour” prints the resulting newlines with an ‘e’ character, which matches any of the following colors available from ASCII C and/or NCSU format numbers. This will change all other characters besides e into a blank line or otherwise leave them unchanged. It cannot match another shell variable that does not appear within its range as defined by this example script string: ${color:#0080ff} These values are interpreted using local special values for some terminals; they may differ slightly depending on your environment.

Fig 1.9 using escape -Blue bg, white fg

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!