4

I would like to preserve colors, in console, of a Bash script output, while logging and echo it to console.

When I run a script with the following code, I lose colors in console.

LOG_FILE="Merge.log"
touch $LOG_FILE

exec > >(tee $LOG_FILE) 2>&1;

Here are screenshots to make it clear:

Screen one and two are with logging disabled (commented out) in the script:

Screen-1:

Screen one

Screen-2:

Screen two

Screen three and four are with logging enabled in the script:

Screen-3:

Screen three

Screen-4:

Screen four

I have searched quite a lot but couldn't find any solution. Thanks.

OS: Ubuntu MATE 21.04

Bash version: 5.1.4(1)-release (x86_64-pc-linux-gnu)

Jags
  • 2,126
  • 2
  • 19
  • 37
  • I don't think there's anything useful in the stackoverflow Q&A that you linked - in fact, if you read the comments, both the posted answers do exactly the *opposite* of what is asked (i.e. *remove* color codes from colored output). The issue here is that whatever processes are writing to your (redirected) stdout / stderr are detecting that their destination is no longer a terminal, and are removing the colors before the data even gets to the `tee` command. – steeldriver Jun 07 '21 at 22:36
  • @steeldriver just removed StackOverflow link. Thanks. – Jags Jun 08 '21 at 20:05
  • Maybe this helps: https://askubuntu.com/questions/647805/how-to-print-the-output-of-tree-to-pdf-without-losing-the-color – Bruni Jun 09 '21 at 08:30
  • @bruni Thanks. Did you mean to say, use `aha`? 'coz I can't seem to figure out, how to use `aha` with `tee`. – Jags Jun 09 '21 at 09:45
  • @Jags yes that is what I meant, but you are right, it would probably not work with tee. I missed that tee also discards the color on the console. – Bruni Jun 09 '21 at 10:08
  • One more try, something in the lines of the accepted answer to this: https://superuser.com/questions/352697/preserve-colors-while-piping-to-tee – Bruni Jun 09 '21 at 11:02
  • @bruni I've just removed my previous comment. At first I thought, it's working but actually I'm getting errors. It's not generating any logs. – Jags Jun 09 '21 at 12:34
  • @bruni The error I'm getting in `Merge.log` and in console as well: `couldn't execute "exec": no such file or directory | while executing | "spawn -noecho exec" | ("eval" body line 1) | invoked from within` `"eval [list spawn -noecho] $argv" | invoked from within | "if {[string compare [lindex $argv 0] "-p"] == 0} {` `# pipeline | set stty_init "-echo" | eval [list spawn -noecho] [lrange $argv 1 end] | clo..." | (file "/usr/bin/unbuffer" line 13)` – Jags Jun 09 '21 at 12:37
  • @Jags was worth a try...Have you tried using `script` like in the accepted answer to this: https://stackoverflow.com/questions/3515208/can-colorized-output-be-captured-via-shell-redirect – Bruni Jun 09 '21 at 12:44
  • 1
    @bruni Yes, I have came across that StackOverflow thread even before posting this question, but I couldn't figure out, how to modify it to my use case to even try it. Thanks. – Jags Jun 09 '21 at 12:54

0 Answers0