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?
sed
outputs the colour codes. Thenrm
runs. Howeversed
is not receiving the stream. Tryecho >(ls)
to see some of what is happening. – ctrl-alt-delor Apr 17 '19 at 16:24