Search code examples
linuxloggingcommand-lineterminalansi-escape

Remove ANSI codes when storing script output


Some programs makes beautiful progressbars and stuff using ANSI escape sequences. That's nice.

What's not nice though is that if i put the output of that kind of program into a file and then try to view it it's filled with strange escape sequences.

Is there a way to strip away all the ANSI codes while logging?

I usually log the output of a script this way:

./script >> /tmp/output.log

Solution

  • Try:

    $ TERM=dumb ./script >> /tmp/output.log
    

    If that doesn't work, it's because the ANSI codes have been hard-coded into the script, so there is no easy way to remove them. If it does, it's because it's doing the right thing, delegating things like pretty output to libncurses or similar, so that when you change the TERM variable, the library no longer sends those codes.