How to Change Terminal Output Color in Linux?


Introduction

The Linux Terminal is a powerful tool that allows users to interact with the operating system through the command line. However, the terminal's default output color can be dull and unattractive. In this article, we will discuss several ways to change the terminal output color in Linux. We'll cover the use of different commands and tools that can be used to customize the terminal's color scheme, as well as some examples of how to use them. This guide is intended for Linux users who want to improve their terminal experience by changing the output color.

Using the "LS" command

One of the easiest ways to change the terminal output color in Linux is by using the "ls" command. The "ls" command is used to list the contents of a directory and has several options that can be used to customize the output. Here is an example of the "ls" command −

$ ls

The output will look like this −

bin@   home/            lib32@       media/  root/  sys/  vmlinuz@
boot/  initrd.img@      lib64@       mnt/    run/   tmp/  vmlinuz.old@
dev/   initrd.img.old@  libx32@      opt/    sbin@  usr/
etc/   lib@             lost+found/  proc/   srv/   var/

Using the "LS_COLORS" environment variable

Another way to change the color of terminal output on Linux is to use the "LS_COLORS" environment variable. This variable can be used to set the color scheme for the "ls" command. The variable can be set in the ".bashrc" file, located in the user's home directory. To set the "LS_COLORS" variable, add the following line to the ".bashrc" file −

$ export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.'

The line above sets the color scheme for different file types. For example, "of" represents the directory and "1;35" represents the color code of the directory. Similarly, "ex" represents the executable and "01;32" represents the color code of the executable.

Using the "Tput" command

The "tput" command can be used to change the color of terminal output on Linux. The "tput" command can be used to set the color scheme of the terminal, as well as change the background and text color. Below is an example of how to use the "tput" command to set the text color to red −

$ tput setaf 1

The above command sets the text color to red. To change the background color, use the "setab" option instead of "setaf". Here is an example of how to use the "tput" command to set the background color to blue −

$ tput setab 4

Using "Gnome-Terminal"

Gnome-Terminal is a terminal emulator for the GNOME desktop environment. It provides several options for customizing the terminal, including the ability to change the terminal output color. To change the color of terminal output in Gnome-Terminal, open the terminal and go to “Edit -> Profile Preferences”. In the "Colors" tab you can change the background and text color of the terminal.

ANSI escape codes

Another method to change the color of terminal output is to use ANSI escape codes. ANSI escape codes are standardized commands used to manipulate the behavior and appearance of text in a terminal or terminal emulator. For example, we can write a script that prints the red words "Hello world" on a green background using the echo command and then resets the colors to normal −

#!/bin/sh
RED_ON_GREEN='\033[31;42m'
RESET='\033[0m'
echo "${RED_ON_GREEN}Hello world${RESET}"

"\33" is the most platform-independent way of encoding an ESC character in the terminal, although a reference, "\e", can also be used on Linux. Also, it's worth noting that the color settings aren't encapsulated in any way. As a result, if we don't restart them, they will bleed out of our script, which is undesirable in most cases. We can find the full list of color codes on Wikipedia.

#!/bin/sh
RED_FG=`tput setaf 1`
GREEN_BG=`tput setab 2`
RESET=`tput sgr0`
echo "${RED_FG}${GREEN_BG}Hello world${RESET}"

tput command

The tput command is another option that can be used to change the color of terminal output on Linux. The tput command allows users to query the terminfo database and provides a convenient way to extract the escape codes we need. For example, we can recreate the script from the previous section using the tput command −

Conclusion

In this article, we have discussed various ways to change the color of terminal output in Linux. We have covered the use of several commands and tools such as the "ls" command, the "LS_COLORS" environment variable, the "tput" command, and Gnome-Terminal to customize the terminal's color scheme. We also discussed using ANSI escape codes and the tput command to change the color of terminal output. Using these methods, you can make your terminal more visually appealing and easier to read. Remember to use commands with the correct syntax and options to change the color of terminal output on Linux.

Updated on: 13-Feb-2023

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements