How to Change Output Color of Echo in Linux

The output color of the echo command in Linux can be changed by using the -e option and adding the ANSI escape code for the desired color.

For example, to change the output color to green, you would use the following command:

echo -e “\033[32mHello World\033[0m”

The \033[32m part of the command sets the text color to green, and the \033[0m part resets the text color back to the default.

You can also use other ANSI escape codes to change the text color to other colors, such as red, blue, yellow, etc.
[ad_1]

The “echo” command is a fundamental tool in Linux that allows you to display text on the terminal. By default, the output color of the “echo” command is black. So that whenever you use it, you have some difficulty. To overcome this think, there are a few ways to change the output color of the “echo” command in Linux. In this tutorial, you will learn how to change the output color of echo in Linux.

Do it before you use any of the methods in this tutorial. It needs to tell you about the syntax of the echo command. The basic syntax of the echo command is as follows:

echo [option(s)] [string(s)]

Here, [option(s)] refers to the various options that can be used with the echo command, such as the -n option to suppress the trailing newline or the -e option to enable interpretation of backslash escapes. [string(s)] refers to the message that you want to display on the terminal.

How to Change Output Color of Echo in Linux

Now, let’s move on to changing the output color of echo in Linux.

  • Method 1: Using ANSI Escape Codes
  • Method 2: Using the tput Command
  • Method 3: Using the PS1 environment variable

Method 1: Using ANSI Escape Codes

ANSI provides a series of escape code control sequences that can be used to modify the color and appearance of text displayed on a Linux terminal.

To use ANSI escape codes with the echo command & need to embed them within the message string using following syntax:

echo -e "3[COLORmMESSAGE3[0m"

Here, \033[ is the escape character sequence that indicates the start of an ANSI escape code. COLOR refers to the color that you want to set, and MESSAGE is the message string that you want to display. \033[0m is the escape code that resets the color to the default.

Let’s look at an example to explain this method, suppose you want to type “Hello, World!” want to display the message. in the red. So, you can achieve this by using the following command:

echo -e "3[31mHello, World!3[0m"

Here, \033[31m sets the color to red, and \033[0m resets the color to the default.

Similarly, you can use other ANSI escape codes to set different colors, such as \033[32m for green, \033[33m for yellow, \033[34m for blue, and so on.

And, You can replace “31” with any of the following values to get different colors:

  • 30: black
  • 31: red
  • 32: green
  • 33: yellow
  • 34: blue
  • 35: magenta
  • 36: cyan
  • 37: white

Method 2: Using the tput Command

The tput command is a utility used to set terminal attributes such as color, cursor position, and screen size. To change the color of the echo command using the tput command, you need to use the following syntax:

echo "$(tput setaf COLOR)MESSAGE$(tput sgr0)"

Here, $(tput setaf COLOR) sets the foreground color to COLOR, and $(tput sgr0) resets the color to the default.

Let’s take an example to illustrate this method. Suppose you want to display the message “Hello, World!” in green color.

You can get this using the following command:

echo "$(tput setaf 2)Hello, World!$(tput sgr0)"

Here, $(tput setaf 2) sets the foreground color to green, and $(tput sgr0) resets the color to the default.

And, You can replace “1” with any of the following values to get different colors:

  • 0: black
  • 1: red
  • 2: green
  • 3: yellow
  • 4: blue
  • 5: magenta
  • 6: cyan
  • 7: white

Method 3: Using the PS1 environment variable

The PS1 environment variable is used to customize the command prompt in Linux. Now, you can also use it to change the output color of the “echo” command. Here is an example:

PS1="\[3[31m\]\[email protected]\h:\w$ \[3[0m\]"

In this example, you set the PS1 variable to include the “\033[31m” sequence to change the output color to red, and “\033[0m” to reset the color back to its default value. You can modify this variable to include any other color sequences that you want.

Conclusion

Changing the output color of the “echo” command in Linux is easy and can be done using several methods. The most common methods include using ANSI escape sequences, the “tput” command, and the PS1 environment variable. By using these methods, you can make the text more readable and enhance your overall experience on the terminal.

Recommended Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00