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

GREEN='\033[0;32m'
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. 

How to Change the Output Color of Echo in Linux

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 to change foreground and background colors in Linux. Some examples showing how to use echo are included on this page.

Similar Reads

What is tput? How does it work?

tput is a command-line utility in Unix-like operating systems that allows users to interact with the terminal’s capabilities, such as cursor movement, text colors, and formatting. It works by sending terminal control sequences to manipulate terminal settings and appearance....

Reading Terminal Attributes

Reading terminal attributes involves retrieving information about the terminal settings, such as their size, supported features, and characteristics. This can be done using various methods, including terminal control codes, environment variables, or terminal query commands....

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

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 the long delay, then there may be something wrong here. The ANSI for various commonly used colors can be seen below....

Add color to the output of printf

We can also use the same to color the 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....

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

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

Syntax of tput

The tput command is used to interact with terminal capabilities, allowing you to perform various actions such as cursor movement, color manipulation, and text formatting. Its syntax is as follows:...

Exit Status

The tput command provides exit status to indicate the success or failure of its execution. Here are the exit status codes commonly used:...

Change the Output Color of Echo in Linux – FAQs

What is Linux tput?...