2

I'm looking for a way to apply custom colors to messages emitted by the rm -i command:

~ $ rm -i file.txt rm: remove regular empty file 'file.txt'? 

I understand that I need to redirect the stderr as stdin to any command that will apply the ANSI colors to it and redirect the output back to stderr, e.g.

~ $ rm -i file.txt 2> >(sed $'s/^/\e[31m/;s/$/\e[m/'>&2) 

However, this alone doesn't work; the above command doesn't print anything and starts to await the user input immediately. If I enter e.g. n and Enter, I get the colored output printed afterwards:

~ $ rm -i file.txt 2> >(sed $'s/^/\e[31m/;s/$/\e[m/'>&2) n rm: remove regular empty file 'file.txt'? ~ $ 

I seem to understand what is going wrong here - the stderr was redirected and colored, but not printed since the command started to await user input. Is there a way to print colored message before rm starts listening to the stdin?

2
  • 2
    Since rm outputs its messages to stderr, the answers to Can I configure my shell to print STDERR and STDOUT in different colors? may help. Commented Apr 17, 2019 at 16:20
  • The reason that it gets coloured, is because sed outputs the colour codes. Then rm runs. However sed is not receiving the stream. Try echo >(ls) to see some of what is happening. Commented Apr 17, 2019 at 16:24

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.