How to Alter the Echo Output Colors in Linux

Avatar

By squashlabs, Last Updated: Oct. 17, 2023

How to Alter the Echo Output Colors in Linux

Introduction

In Linux, the bash shell provides various ways to alter the output color of the echo command. Changing the color of the text can be useful for emphasizing certain information or organizing the output in a more visually appealing way. This guide will explain several methods to achieve this in the bash shell.

Related Article: Indentation Importance in Bash Scripts on Linux

Method 1: Using ANSI Escape Sequences

One common approach to alter the echo output color in Linux is by using ANSI escape sequences. These sequences are a set of characters that, when included in the echo command, modify the color and formatting of the output.

To change the color of the text, you can use the following syntax:

echo -e "\e[COLOR_CODEmYour Text\e[0m"

Replace COLOR_CODE with the desired color code. Here are some commonly used color codes:

- Black: 0;30

- Red: 0;31

- Green: 0;32

- Yellow: 0;33

- Blue: 0;34

- Magenta: 0;35

- Cyan: 0;36

- White: 0;37

For example, to display the text "Hello, World!" in red, you would use:

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

Method 2: Using tput

Another approach to alter the echo output color is by using the tput command. tput is a utility that allows you to modify terminal settings, including text colors.

To change the color of the text using tput, you can use the following syntax:

echo "$(tput setaf COLOR_CODE)Your Text$(tput sgr0)"

Replace COLOR_CODE with the desired color code. tput setaf sets the foreground color, and tput sgr0 resets the color back to the default.

Here are some commonly used color codes:

- Black: 0

- Red: 1

- Green: 2

- Yellow: 3

- Blue: 4

- Magenta: 5

- Cyan: 6

- White: 7

For example, to display the text "Hello, World!" in green, you would use:

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

Best Practices and Additional Considerations

- When altering the echo output color, it's important to choose colors that are easy to read and visually appealing. Avoid using colors that may be difficult to distinguish, especially for users with visual impairments.

- Consider using color combinations that provide good contrast, such as white text on a black background or vice versa.

- Use color sparingly and purposefully. Too many different colors can make the output confusing and harder to read.

- If you frequently use the same color combinations, consider defining them as shell variables for easier reuse. For example:

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)

echo "${RED}Error:${RESET} Something went wrong."
echo "${GREEN}Success:${RESET} Operation completed successfully."
echo "${YELLOW}Warning:${RESET} Proceed with caution."

More Articles from the The Linux Guide: From Basics to Advanced Concepts series:

Making Bash Scripts Executable with Chmod in Linux

Learn how to modify permissions in Linux to make bash scripts executable using chmod. Discover the different permissions in Linux, check the current … read more

Executing Scripts in Linux Without Bash Command Line

Executing scripts in Linux without using the bash command line is a topic that software engineers often encounter. This article explores various alte… read more

Bash Scripting Handbook for Linux SysAdmins

This comprehensive resource, the "This guide," is designed to help Linux sysadmins navigate the world of bash scripting. From getting started with ba… read more

How to Hide & Validate User Input in Bash Scripts

Securing user input in bash scripts is a critical aspect of Linux system administration. This article covers techniques to hide and validate user inp… read more

Scheduling Bash Scripts in Linux: Cron, Crontab & More

Learn how to schedule bash scripts to run at specific times in Linux. This article covers topics such as understanding cron and crontab, using the 'a… read more

How to Manipulate Quotes & Strings in Bash Scripts

A concise tutorial on manipulating quotes and strings in Bash scripts in a Linux environment. Learn how to add or remove quotes from strings, perform… read more

Executing Configure Read from Bash Script in Linux

The article provides a comprehensive look at how to call configure read from a bash script in Linux. This article covers various topics such as using… read more

Executing Bash Scripts Without Permissions in Linux

This guide explores various methods to run bash scripts in a Linux system without requiring permissions. The article covers topics such as executing … read more

Secure File Transfer with SFTP: A Linux Tutorial

Learn how to securely transfer files with a remote server using SFTP in Linux. This tutorial covers everything from installing and configuring SFTP t… read more

Comparing Array Sizes in Bash Scripting on Linux

Learn how to compare array sizes in bash scripting on Linux with a variety of code snippets and commands. Discover different approaches such as using… read more