How to Change Output Color of Echo in Linux

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 thing, 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 Command Line

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

  • 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\]\u@\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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *