Colouring stderr in terminal

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
afora
Level 4
Level 4
Posts: 237
Joined: Mon Aug 26, 2019 7:35 pm

Colouring stderr in terminal

Post by afora »

Hi guys,

I wanted to colour red all stderr to quickly differentiate between different messages in one of my LM setup scripts. I found this one online which sort of works fine but not quite:

Code: Select all

# Colour stderr red for bash terminal
export COLOR_RED="$(tput setaf 1)"
export COLOR_RESET="$(tput sgr0)"
exec 9>&2
exec 8> >(
    perl -e '$|=1; while(sysread STDIN,$a,9999) {print
"$ENV{COLOR_RED}$a$ENV{COLOR_RESET}"}'
)
function undirect(){ exec 2>&9; }
function redirect(){ exec 2>&8; }
trap "redirect;" DEBUG
PROMPT_COMMAND='undirect;'
The problem with it is that it colours red also some irrelevant messages (like if I do $read "Press Enter to continue"), and also the most important it prevents me to run $sudo su as terminal would simply hang. I have to literally comment out the code in .bashrc and restart terminal before being able to elevate myself to root.

I have no idea what this piece of code does frankly. Could anyone suggest either how to fix it or may be there's an easier way to accomplish this.

Many thanks!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Colouring stderr in terminal

Post by xenopeek »

A quick test shows that read outputs the prompt on stderr, hence it gets colored with your setup:

Code: Select all

$ read -p "Press enter to continue" 1>/dev/null
Press enter to continue
# ^ text was shown so stdout isn't used for outputting the prompt

$ read -p "Press enter to continue" 2>/dev/null

# ^ no text was shown so stderr IS used for outputting the prompt, hence it is red with your setup
A workaround for that specific case is to redirect stderr for the command to stdout:
read -p "Press enter to continue" 2>&1
With this it won't color the prompt.

I only ran your commands in a shell, didn't put them in my .bashrc. With that it also hangs on sudo -i for me but also if I simply run bash to start a new shell. Does it also hang for you if you simply run bash?

Where did you find this code. Perhaps the discussion on that page provides more clues.
Image
afora
Level 4
Level 4
Posts: 237
Joined: Mon Aug 26, 2019 7:35 pm

Re: Colouring stderr in terminal

Post by afora »

Fantastic, thank you for resolving the very first question. I should have thought about it myself!

As to the second point - I got it from a now defunct forum threat. I do not think I understand what the code is doing, so I thought I would ask it here. It's a really great solution when one is running a whole bunch of scripts in an automated fashion to distinguish between failures and normal execution.

Perhaps any suggestions from anybody who's seasoned here?

Many thanks!
Locked

Return to “Scripts & Bash”