14

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 ?

2
  • This link might help you
    – devav2
    Sep 17, 2012 at 18:30
  • You can enable this behavior for one command using the hilite utility. You have to manually prepend hilite to your shell command to use it, but it’s still useful if you know or suspect that a command outputs errors and you want to locate them. Mar 25, 2014 at 21:37

2 Answers 2

9

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

1
  • Can't believe I went so many years without knowing that this exists! It's amazingly useful and should have been part of the OS for, oh, a hundred years or so by now. :)
    – Roger Dahl
    Jul 25, 2019 at 18:55
6

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.

1
  • is there anyway to make it colorized any err message from any program?
    – Luke
    Mar 6, 2019 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .