Is there a way to make bash display stderr messages in red color?

link|improve this question

I guess bash will never colorize its output: some program may want to parse something, and colorizing will spoil data with escaped sequences. A GUI app should handle colors, i guess. – o_O Tync Aug 26 '09 at 22:20
feedback

5 Answers

up vote 15 down vote accepted
command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)
link|improve this answer
Great! But i wonder if there's a way to make it permanent :) – o_O Tync Aug 26 '09 at 21:47
#Tync: You would replace 'command' with 'exec', but that will break the prompt, and may also break other commands that use stderr interactively (rm -i, for example). – Juliano Aug 26 '09 at 22:10
Thanks very much. Have your tenth '+1'. – Drew Noakes Jul 2 '11 at 20:48
thanks for the +1 :) – Balázs Pozsár Jul 22 '11 at 23:45
feedback

You can also check out stderred: https://github.com/sickill/stderred

link|improve this answer
Wow, this utility is great, the only thing that it would need is to have an apt repository that installs it for all users, with one line, not having to do more work to enable it. – Sorin Sbarnea Apr 3 at 14:17
feedback

http://sourceforge.net/projects/hilite/

link|improve this answer
brilliant! (15 char limit) – LiraNuna Aug 26 '09 at 22:15
feedback

I've made a wrapper script that implements Balázs Pozsár's answer in pure bash. Save it in your $PATH and prefix commands to colorize their output.


    #!/bin/bash

    if [ $1 == "--help" ] ; then
        echo "Executes a command and colorizes all errors occured"
        echo "Example: `basename ${0}` wget ..."
        echo "(c) o_O Tync, ICQ# 1227-700, Enjoy!"
        exit 0
        fi

    # Temp file to catch all errors
    TMP_ERRS=$(mktemp)

    # Execute command
    "$@" 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" | tee --append $TMP_ERRS; done)
    EXIT_CODE=$?

    # Display all errors again
    if [ -s "$TMP_ERRS" ] ; then
        echo -e "\n\n\n\e[01;31m === ERRORS === \e[0m"
        cat $TMP_ERRS
        fi
    rm -f $TMP_ERRS

    # Finish
    exit $EXIT_CODE

link|improve this answer
1  
This could be made more efficient if "|tee..." was put after "done". – Juliano Aug 27 '09 at 1:27
feedback

You can use a function like this


 #!/bin/sh

color() {
      printf '\033[%sm%s\033[m\n' "$@"
      # usage color "31;5" "string"
      # 0 default
      # 5 blink, 1 strong, 4 underlined
      # fg: 31 red,  32 green, 33 yellow, 34 blue, 35 purple, 36 cyan, 37 white
      # bg: 40 black, 41 red, 44 blue, 45 purple
      }
string="Hello world!"
color '31;1' "$string"

link|improve this answer
3  
Not addressing the problem. You haven't provided a way of separating stderr from stdout, which is what the O.P. is interested in. – Jeremy Visser Aug 27 '09 at 1:54
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.