How to set font color for STDIN,STDOUT and STDERR?

I want to differentiate the STDOUT and STDERR messages in my terminal .
If a script or command is printing a message in terminal I want to differentiate by colors,
Is it possible ?

Example:

$date
Wed Jul 27 12:36:50 IST 2011
$datee
bash: datee: command not found
$alias ls
alias ls='ls --color=auto -F '
$aliass ls
bash: aliass: command not found

The terminal can't tell what's printing to standard output vs standard error -- they both go to literally the same file. You could intercept standard error with a fifo and process it...

#!/bin/bash

# On exit, close stderr and delete the fifo so we don't leave dangling background processes.
trap "exec 2<&- ; rm ~/.tmpfifo" EXIT

rm -f ~/.tmpfifo

mkfifo ~/.tmpfifo
# Create a subshell that reads from fifo, writes to stderr.
# it gets its own copy of stderr, so redirecting stderr later won't change this.
(       while read LINE
        do
                printf "\x1b[01;31m%s\x1b[01;m\n" "$LINE"
        done < ~/.tmpfifo ) &

exec 2>~/.tmpfifo

# Thing that should print standard error in red
touch /
$ ./errfilter.sh
touch: setting times of `/': Permission denied
$

This only works for scripts, not for interactive terminals, because bash frequently uses stderr for its own things when in interactive mode.

---------- Post updated at 10:27 AM ---------- Previous update was at 10:11 AM ----------

Actually, here's a way that'll work in interactive mode:

trap "exec 5>~/.tmpfifo-$$ ; rm -f ~/.tmpfifo-$$ ; exec 5<&-" EXIT

mkfifo ~/.tmpfifo-$$

(       while [ -e ~/.tmpfifo-$$ ]
        do
                while read LINE
                do
                        printf "\x1b[01;31m%s\x1b[01;m\n" "$LINE"
                done < ~/.tmpfifo-$$ >&2
        done
 ) &

function err
{
        "$@" 2>/.tmpfifo-$$
}

Do this in your shell:

$ source errfilter.sh
$ err touch /
touch: setting times of `/': Permission denied
$

and any commands you run with err will have stderr color-coded.

1 Like

I found this, if it can help somehow, too.

Other than that, another, very simple example (from the WWW):

command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)