Is it even achievable?
We need STDERR
(ie. other streams then STDOUT
) to have different colo(u)r. For example red.
We use bash
, terminal should be Konsole
(XTerm, gnome terminal or any usable).
Thanks if you know :-)
Is it even achievable?
We need STDERR
(ie. other streams then STDOUT
) to have different colo(u)r. For example red.
We use bash
, terminal should be Konsole
(XTerm, gnome terminal or any usable).
Thanks if you know :-)
Hilite will do this. It's a lightweight solution, but you have to invoke it for each command, eg.
hilite gcc myprog.c
. A more radical approach is built in to my experimental shell Gush which shows stderr from all commands run in red, stdout in black. Either way is very useful for software builds where you have lots of output with a few error messages that could easily be missed if not highlighted.Simple solution to color STDOUT in red with
grep
:This should work without installing anything more. (
grep
should be already installed everywhere.)Taken here from Dennis's comment on superuser.com.
Here's a solution that combines some of the good ideas already presented.
Create a function in a bash script:
Use it like this:
It will show the command's
stderr
in red.Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
color()...
— Creates a bash function called color.set -o pipefail
— This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell."$@"
— Executes the arguments to the function as a new command."$@"
is equivalent to"$1" "$2" ...
2>&1
— Redirects thestderr
of the command tostdout
so that it becomessed
'sstdin
.>&3
— Shorthand for1>&3
, this redirectsstdout
to a new temporary file descriptor3
.3
gets routed back intostdout
later.sed ...
— Because of the redirects above,sed
'sstdin
is thestderr
of the executed command. Its function is to surround each line with color codes.$'...'
A bash construct that causes it to understand backslash-escaped characters.*
— Matches the entire line.\e[31m
— The ANSI escape sequence that causes the following characters to be red&
— Thesed
replace character that expands to the entire matched string (the entire line in this case).\e[m
— The ANSI escape sequence that resets the color.>&2
— Shorthand for1>&2
, this redirectssed
'sstdout
tostderr
.3>&1
— Redirects the temporary file descriptor3
back intostdout
.Here's an extension of the same concept that also makes STDOUT green:
You can also check out stderred: https://github.com/sickill/stderred
I can't see that there is any way for the terminal emulator to do this.
The interface between the terminal emulator and the shell/app is via a pseudo-tty, where the terminal emulator is on the master side and the shell/app on the other. The shell/app have both stdout and stderr connected to the same pty, so when the terminal emulator reads from the pty for the shell/app output it can no longer tell which was written to stdout and which to stderr.
You will have to use one of the solutions that intercepts the data between the application and the slave-pty and inserts escape codes to control the terminal output colo(u)r.
Here is a little Awk script that will print everything you pass it in red.
It simply prints each line it receives on stdin within the necessary escape codes to display it in red. It is followed by an escape code to reset the terminal.
(If you need a different colour, change the 31)
Save it to colr.awk, do a
chmod a+x
, and use it like so:It has the drawback that lines may not be displayed in order, because
stderr
goes directly to the console, whilestdout
first goes through an additional process.