1. Overview

In this tutorial, we’ll cover some tools we can use to write standard output to files while preserving their colors. This is especially useful when debugging, as the colored parts make it easier to scan the output logs.

2. Using tee

The tee command is a utility that reads standard input and writes to both the standard output and one or more files at the same time. The command gets its name from the T-splitter used in plumbing.

We’ll use it with a combination of other commands through piping.

Here’s the syntax for the tee command:

$ tee [option]... [file]...

We’ll use the echo command to produce some colored output, then pipe it with the tee command to save the output:

$ echo -e "\e[1;32m Baeldung is awesome \e[0m" | tee -a tee_output.txt

We get this output:

tee 1

Let’s breakdown the command to understand it:

  • echo -e: using echo with -e flag to output our string with the interpretation of backslash escapes enabled
  • “\e;32m … \e[0m”: ANSI escape sequence converts the text to green. We’ve used the terminal color code “32”, which changes foreground color to green
  • tee -a: creates a file if it doesn’t exist  and appends the text to it

We’ve used tee to save colored output from an echo command as a basic example. Similarly, we can pipe tee with other commands that produce colored output or logs.

3. Using grep

grep is an essential command to search for patterns of text or strings in a file. The patterns are referred to as regular expressions.

Here’s the syntax for the grep command:

$ grep [options] pattern [files]

Let’s create a sample.txt file that we’ll use with grep:

$ cat >> sample.txt

Baeldung is awesome!!!

Next, let’s use grep to colorize our matched string and then direct the output to a different file:

$ grep --color=always "awesome" sample.txt > grep_output.txt

Here’s the result when we view the contents of the output file using cat:

grep 1

We used –color=always with grep to preserve ANSI sequences when directing the output to a file. By default, grep strips off colors from the output when piped to a file.

4. Using script

script is a command used to make a typescript of all terminal activities. It records all activity, including inputs and outputs, until we stop it.

It saves the data in the typescript file by default. But we can pass an argument to save the output to a different file.

The script command uses two files: one for terminal outputs and the other for the timing information.

Let’s see the syntax for the script command:

$ script [options] [file]

To preserve ANSI characters while saving to file, let’s use this command:

$ script -q /dev/null -c "echo -e '\e[1;32m Baeldung is awesome \e[0m'" > script_output.txt

This won’t give us any output, but we can use cat to view the contents of the created file:

script 1

A breakdown of this command to understand more:

  • -q: –quiet option, ensures start and done messages are not written to standard output
  • /dev/null: we’ve used this path to utilize the script tag and discard the output file at the same time
  • ‘\e[1;32m … \e[0m’: ANSI escape sequence that converts the text to green. We’ve used the terminal color code “32”, which changes foreground color to green

We can substitute the echo command with any other command that produces colored output.

5. Using echo

The echo command displays text characters that we pass as arguments.

We can use echo with a combination of escape sequences that specify the color of text on our file:

$ echo -e "\e[1;32m Baeldung is awesome \e[0m" > echo_output.txt

This saves the text to the echo_output.txt file with a green color. Let’s use cat to view it:

$ cat echo_output.txt
echo 1

We used echo with the -e flag to enable the processing of backslash escape sequences. As before, the terminal color code “32” is used to change the text to green.

6. Using ansi2html

ansi2html is a Linux program that converts ANSI log to a pretty HTML page. It also converts ANSI color codes to HTML tags. By default, it supports all standard colors and attributes.

ansi2html isn’t available by default, but we can install it through the package manager.

Let’s use this command to install on Debian, Ubuntu, or Kali:

$ sudo apt-get install colorized-logs

On Red Hat, we can use yum:

$ yum install colorized-logs

After installation, we can use echo to produce colored output, then pipe it with ansi2html to save the result to an HTML file:

$ echo -e "\e[1;32m Baeldung is awesome \e[0m" | ansi2html > ansi_output.html

This creates a new ansi_output.html file that we can preview using any web browser:

ansi2html 3 1

7. Using aha

aha (ANSI HTML Adapter) is a Linux tool that converts ANSI escape sequences from terminal to HTML. It was developed by Alexander Matthes and is supported by most major Linux distributions.

aha isn’t available by default, but we can install it through the package manager.

Let’s use this command to install on Debian, Ubuntu, or Kali:

$ sudo apt-get install aha

On Red Hat, we can use yum:

$ yum install aha

After installation, we can convert the colored output to HTML and save it to a file:

$ echo -e "\e[1;32m Baeldung is awesome \e[0m" | aha --black > aha_output.html

Here is a preview of the HTML file created. We can view it through a web browser:

1 aha 1

We used the –black flag to set the background color of the page to black.

8. Conclusion

In this article, we’ve covered some tools that we can use to preserve colors when saving stdout or standard output to a file.

Comments are closed on this article!