Skip to content

Instantly share code, notes, and snippets.

@pokle
Last active September 21, 2017 08:12
Show Gist options
  • Save pokle/d3e3e364b7850b7f3042dca1677378bb to your computer and use it in GitHub Desktop.
Save pokle/d3e3e364b7850b7f3042dca1677378bb to your computer and use it in GitHub Desktop.
How to indent the output of a shell colorising stderr red
#!/usr/bin/env bash
#
# Based on http://stackoverflow.com/questions/9112979/pipe-stdout-and-stderr-to-two-different-processes-in-shell-script
set -e
function indented() {
echo RUnNINg: $@
(set -o pipefail; { "$@" 2>&3 | sed >&2 's/^/ | /'; } 3>&1 1>&2 | perl -pe 's/^(.*)$/\e[31m | $1\e[0m/')
}
function die() { echo ERROR: $@ > /dev/stderr; exit 1; }
echo Welcome to the indented running example.
echo
echo Here is a curl run:
indented curl -v http://example.com
echo
echo Here\'s a pipeline
indented bash -c 'echo -n abc | wc -c'
@pokle
Copy link
Author

pokle commented Sep 21, 2017

Or even just sed!

ls -l | sed 's/^/   --| /'
   --| total 784
   --| -rw-r--r--     1 tpokle  SEEK\Domain Users    2377 21 Sep 17:04 Makefile
   --| -rw-r--r--     1 tpokle  SEEK\Domain Users     349 20 Sep 14:55 Makefile.development.cf
   --| -rw-r--r--     1 tpokle  SEEK\Domain Users     681 20 Sep 14:55 Makefile.production.cf
   --| -rw-r--r--     1 tpokle  SEEK\Domain Users    9181 20 Sep 14:55 README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment