0

I want to save the output from a script so I can view it later. However, when I save the output to a file (script > some/file) and view it later, there's no color, even though script originally outputs in several colors to make the output easier to read.

It makes sense that the color isn't saved since the resulting file is just plain text, but is there any way I can take any given script, and reproduce the output later with the same styling without invoking the script again?

2

Some programs (including whatever you put in your script) detect whether the output is a terminal or a file, and turn off colors for that case. If you run your script using the program script, that avoids this problem, by capturing all of the characters into a file named typescript, e.g.,

script -c script

(where the latter is of course, your script) and later

cat typescript

Depending on the system, the script program may use different arguments. The first (defaulting to typescript) is where the script program writes its output:

  • the one on Debian/Ubuntu/etc is in a package named "bsdutils", and the command must be given using a -c option, e.g, "script -c script".

  • on a BSD system, there is no -c option, and the command can be given be given as parameters after the file, e.g,. "script typescript script".

Though supported on (probably) every POSIX system, script is not part of POSIX.

  • On my distribution of Linux (Ubuntu 16.04) the syntax was a little different. I could accomplish this with echo "scriptName ; exit" | script test where "test" is the name of the file to store the output, and "scriptName" is the name of the script I want to store the output for. – Skeets Apr 7 at 23:10
0

You can use the less -R <filename>.<fileformat> command to open your output file. It should display the colors...

Just in case here is an example of colored echo:
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"

Also, you can use html file as output file and then open it in your browser:
echo "<div style='color:$COLOR;'>$SOMEVARIABLE</div><br>" > output.html
(the color should be in HEX format, for example #040404)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.