Can colorized output be captured via shell redirect? [duplicate]
One way to capture colorized output is with the script
command. Running script
will start a bash session where all of the raw output is captured to a file (named typescript
by default).
Redirecting doesn't strip colors, but many commands will detect when they are sending output to a terminal, and will not produce colors by default if not. For example, on Linux ls --color=auto
(which is aliased to plain ls
in a lot of places) will not produce color codes if outputting to a pipe or file, but ls --color
will. Many other tools have similar override flags to get them to save colorized output to a file, but it's all specific to the individual tool.
Even once you have the color codes in a file, to see them you need to use a tool that leaves them intact. less
has a -r
flag to show file data in "raw" mode; this displays color codes. edit: Slightly newer versions also have a -R flag which is specifically aware of color codes and displays them properly, with better support for things like line wrapping/trimming than raw mode because less can tell which things are control codes and which are actually characters going to the screen.