Score:4

Preserve colors in console of a Bash script output while logging

kp flag

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)

hr flag
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.
Jags avatar
kp flag
@steeldriver just removed StackOverflow link. Thanks.
Bruni avatar
cn flag
Maybe this helps: https://askubuntu.com/questions/647805/how-to-print-the-output-of-tree-to-pdf-without-losing-the-color
Jags avatar
kp flag
@bruni Thanks. Did you mean to say, use `aha`? 'coz I can't seem to figure out, how to use `aha` with `tee`.
Bruni avatar
cn flag
@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 avatar
cn flag
One more try, something in the lines of the accepted answer to this: https://superuser.com/questions/352697/preserve-colors-while-piping-to-tee
Jags avatar
kp flag
@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 avatar
kp flag
@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)`
Bruni avatar
cn flag
@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
Jags avatar
kp flag
@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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.