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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
hoefling
  • 886
  • 1
  • 7
  • 13
  • 2
    Since rm outputs its messages to stderr, the answers to [Can I configure my shell to print STDERR and STDOUT in different colors?](https://unix.stackexchange.com/questions/12439/can-i-configure-my-shell-to-print-stderr-and-stdout-in-different-colors) may help. – Mark Plotnick Apr 17 '19 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. – ctrl-alt-delor Apr 17 '19 at 16:24

0 Answers0