Bash – How to capture text formatting in bash

bashcolorsescape-charactersscriptingshell-script

The following shell script works but removes colored formatting generated by rspec:

#!/bin/bash
OUTPUT=`rspec`
echo "$OUTPUT"

How to preserve the colors?

Best Answer

It's common for programs with colorized output to disable it if they're not being run directly in a TTY, since you might be piping the output to a log file or to another process that expects plain text. Typically the programs offer a switch to manually force colors enabled, and rspec has one (--color), but for some reason it ignores it if you're not running in a TTY, which is really unusual behavior.

I think your only options are to edit rspec to take out that check (see def color in rspec-core-2.11.1/lib/rspec/core/configuration.rb), or run it within a program that will trick it into thinking it has a TTY, like expect

Related Question