Is there a way to make bash display stderr messages in red color?
Tell me more
×
Server Fault is a question and answer site for
professional system and network administrators. It's 100% free, no registration required.
|
|
|||||||||||||||
|
|
|
|||
|
|
|
You can also check out stderred: https://github.com/sickill/stderred |
|||||
|
|
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
|
|||||
|
|
Create a function in a bash script:
Use it like this:
It will show the command's Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
|
|||
|
|
|
You can use a function like this
|
|||||
|
|
I have a slightly modified version of O_o Tync's script. I needed to make these mods for OS X Lion and it's not perfect because the script sometimes completes before the wrapped command does. I've added a sleep but I'm sure there's a better way.
|
|||
|
|