Bash profiling

From wikinotes

print lines as executed

See https://unix.stackexchange.com/questions/39644/how-can-i-profile-a-shell-script

# -x: enable xtrace
# -v: enable verbose mode (print line as executed)

set +xv  # before code to profile
set -xv  # following code to profile

exec-time per line

Sometimes (such as within a slow shell startup) it is useful to profile a bash script line-by-line.

Put this at the start of your code that you want to begin profiling

PS4="+ $(date "+%s.%N")\011 "
exec 3>&2 2>/tmp/bashstart.$$.log
set -x

Put this at the end of your code that you want to begin profiling:

set +x
exec 2>&3 3>&-

This will output a file in /tmp/bashstart.<pid>.log. The start-time of each line is recorded (with nanoseconds). The difference between the next line and the first is the time it took to run each line.