Skip to content

Instantly share code, notes, and snippets.

@joshuaboud
Last active May 24, 2022 15:20
Show Gist options
  • Save joshuaboud/956991c04e45b211f40e6da84b2e049e to your computer and use it in GitHub Desktop.
Save joshuaboud/956991c04e45b211f40e6da84b2e049e to your computer and use it in GitHub Desktop.
Colorize stderr
# Colorize stderr output for any bash script
# to use, put the following line at the top of your script (after the shebang):
# source <(curl -s https://gist.githubusercontent.com/joshuaboud/956991c04e45b211f40e6da84b2e049e/raw/colorize_stderr.sh) && exec 2> >(colour_errors >&2)
# or to avoid pulling remotely, copy the following functions into your script followed by:
# exec 2> >(colour_errors >&2)
print_red() {
printf "\e[31;1m$1\e[0m"
}
print_yellow() {
printf "\e[33;1m$1\e[0m"
}
print_orange() {
printf "\e[33m$1\e[0m"
}
print_purple() {
printf "\e[35;1m$1\e[0m"
}
colour_errors() {
perl -pne "s/(err(?:or)?:)/$(print_red '\\1')/gi;s/(warn(?:ing)?:)/$(print_orange '\\1')/gi;s/(^|\\033\[0m)(.+?)(?=\\033|$)/\1$(print_yellow '\\2')/gi"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment