5

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)

10
  • 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. Jun 7, 2021 at 22:36
  • @steeldriver just removed StackOverflow link. Thanks.
    – Jags
    Jun 8, 2021 at 20:05
  • Maybe this helps: askubuntu.com/questions/647805/…
    – Bruni
    Jun 9, 2021 at 8: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 9, 2021 at 9:45
  • 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 9, 2021 at 12:54

0

You must log in to answer this question.

Browse other questions tagged .