How to write Stdout to a file with colors?

When working with command-line tools that produce colorized output, preserving these colors when redirecting to files can be challenging. By default, most programs detect when their output is redirected and strip away ANSI color codes. This article covers various methods to capture colored terminal output in files while maintaining the original formatting.

Using Grep with Color Preservation

The grep command searches for text patterns using regular expressions. To preserve colors when redirecting output, use the --color=always option.

Syntax

$ grep [options] pattern [files]

Example

First, create a sample file and then use grep to search with preserved colors ?

$ cat >> example.txt
tutorials point rocks

$ grep --color=always "tutorials point rocks" example.txt > grep_output.txt
$ cat grep_output.txt
tutorials point rocks

The --color=always option forces grep to retain ANSI color sequences even when output is redirected to a file. Without this option, grep automatically removes colors when piped or redirected.

Using Tee Command

The tee command reads from standard input and simultaneously writes to both standard output and one or more files, preserving colors naturally.

Syntax

$ tee [option]?[file]?

Example

Create colored output using echo and pipe it to tee for file storage ?

$ echo -e "\e[1;32m Tutorial point rocks \e[0m" | tee -a tee_output.txt
Tutorial point rocks

The tee command preserves colors because it acts as a passthrough, maintaining the original data stream without modification. The -a flag appends to the file instead of overwriting.

Using Echo with ANSI Codes

The echo command can generate colored text using ANSI escape sequences and redirect them directly to files.

Example

$ echo -e "\e[1;45m tutorials point rocks \e[0m" > echo_output.txt
$ cat echo_output.txt
tutorials point rocks

Common ANSI color codes include: \e[1;31m (red), \e[1;32m (green), \e[1;34m (blue), and \e[0m (reset). The -e flag enables interpretation of backslash escapes.

Using Script Command

The script command creates a typescript of terminal sessions, capturing all activity including colors and formatting. It records both inputs and outputs exactly as they appear on screen.

Syntax

$ script [options] [file]

Example

Run a command through script to capture colored output ?

$ script -q /dev/null -c "echo -e '\e[1;45m tutorials point rocks \e[0m'" > script_output.txt
$ cat script_output.txt
tutorials point rocks

The -q option runs script in quiet mode, /dev/null discards the default typescript file, and -c executes the specified command. This method is particularly useful for capturing complex command outputs with multiple color changes.

Comparison of Methods

Method Best Use Case Advantages Limitations
grep --color=always Search results Simple, built-in option Only works with grep
tee Live monitoring Shows output while saving Requires piping
echo with ANSI Custom colored text Direct control over colors Manual color coding needed
script Complex terminal sessions Captures everything exactly May include extra formatting

Conclusion

Preserving colors when redirecting stdout to files requires specific techniques depending on your needs. The tee command offers the most versatile solution for real-time colored output capture, while script provides comprehensive session recording. Choose the method that best fits your workflow and output requirements.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements