How to print standard error in red

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 ?


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


In general yes, since Gnome Terminal is an ANSI terminal and supports the standard ANSI escape codes.

Here is an example. Type this in your terminal:

echo -e "\e[01;31mREDRUM\e[0m"

The -e option allows to interpret backslash codes, so that \e becomes "Esc" (hex code 0x1B). Alternatively, to enter escape directly, press ctrl-V Esc:

echo  "^[[01;31mREDRUM^[[0m"

Escape and [ both form together the code that is recognized by the terminal. After that sequence, you need to insert a colon (;) separated list of attributes, terminated by m. See here for all the codes. Examples include attribute 0 (reset all attributes, as you might notice above that is the sequence that terminates my example) and a bunch of style (underlined, bright, bold etc.) as well as a few color attributes.

The answer above tells you how to use color when you write your own program.

However, if I understand your question correctly, you would like to have a visual separation for stderr and stdout of any programs you are running. This is not straightforward without parsing their output through a filter. Here is an example (found here) how to do it:

  1. Define a function (you can put it in your .bashrc file):

    color() { "$@" 2>&1>&3|sed 's,.*,\x1B[31m&\x1B[0m,'>&2; } 3>&1
    
  2. Start programs like this:

    color program
    

The stderr from the programs will get colored red.