Performance profiling tools for shell scripts

You can set PS4 to show the time and line number. Doing this doesn’t require installing any utilities and works without redirecting stderr to stdout.

For this script:

#!/bin/bash -x
# Note the -x flag above, it is required for this to work
PS4='+ $(date "+%s.%N ($LINENO) ")'
for i in {0..2}
do
    echo $i
done
sleep 1
echo done

The output looks like:

+ PS4='+ $(date "+%s.%N ($LINENO) ")'
+ 1291311776.108610290 (3) for i in '{0..2}'
+ 1291311776.120680354 (5) echo 0
0
+ 1291311776.133917546 (3) for i in '{0..2}'
+ 1291311776.146386339 (5) echo 1
1
+ 1291311776.158646585 (3) for i in '{0..2}'
+ 1291311776.171003138 (5) echo 2
2
+ 1291311776.183450114 (7) sleep 1
+ 1291311777.203053652 (8) echo done
done

This assumes GNU date, but you can change the output specification to anything you like or whatever matches the version of date that you use.

Note: If you have an existing script that you want to do this with without modifying it, you can do this:

PS4='+ $(date "+%s.%N ($LINENO) ")' bash -x scriptname

In the upcoming Bash 5, you will be able to save forking date (but you get microseconds instead of nanoseconds):

PS4='+ $EPOCHREALTIME ($LINENO) '

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)