Ubuntu – How to print standard error in red

gnome-terminal

How can i print the standard error in red into the console instead of using the same color of the standard output?

Is it possible using Gnome Terminal ?

Best Answer

I use stderred and have found it a good solution. As its readme notes:

Stderred hooks on write() and a family of stream functions (fwrite, fprintf, error...) from libc in order to colorize all stderr output that goes to terminal thus making it distinguishable from stdout. Basically it wraps text that goes to file with descriptor "2" with proper ANSI escape codes making text red.

It's implemented as a shared library and doesn't require recompilation of existing binaries thanks to preload/insert feature of dynamic linkers.

It's supported on Linux (with LD_PRELOAD), FreeBSD (also LD_PRELOAD) and OSX (with DYLD_INSERT_LIBRARIES).

It is straightforward to compile, but you do need to build it from source by following the instructions from its Github site:

sudo apt-get install build-essential git cmake 

Then

git clone git://github.com/sickill/stderred.git
cd stderred

Then

make

The most important part of it is to add the appropriate line to your .bashrc; you must link to the libstderred.so file in the build directory; you must use the absolute path where the build directory is (/home/mike/src/stderred/build). I add the following to my .bashrc:

export LD_PRELOAD="/home/mike/src/stderred/build/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"

Obviously, when you don't want to use it anymore, remove the above line from your .bashrc and restart the terminal.

The results, tested on non-existent files (it obviously will not work when sudo is used, as the user's .bashrc will not be read when the different environment is set):

(By the way it doesn't turn my duke@nukem prompt red as that was red already)

enter image description here