Last Updated: February 25, 2016
·
1.162K
· nickjacob

prefix command output in bash

When I write scripts that call other programs, like to prefix their output for the user to make it easier to see what's going on. There's a pretty simple way to do this with perl:

prefix_echo () {
  perl -sne '$|=1; print "[$pre]\t$_"' -- -pre="$1"
}

# usage
long_command "1" "2" | prefix_echo "long_command"

# example output
[long_command]    run completed in 0.2s
[long_command]    run completed in 0.8s

1 Response
Add your response

Fantastic tip!

over 1 year ago ·