← Back to Index
DRAFT: This article is a work in progress

Contents

Profile slow bashrc

I have been growing my bashrc bits by bits for years now, and coming back to an older computer made me realize how slow spawning a new terminal is.

Here is how I was able to bring down my 2 seconds terminal boot time to below 200ms.

The core idea

With bash, you have:

So basically, the profiling happens by:

  1. emitting timestamps, and
  2. computing deltas.

Tracing script

Save this as /tmp/profile-bashrc-xtrace.sh:

#!/usr/bin/env bash
# Open a dedicated FD for xtrace output (robust against FD reuse)
exec {__XFD}>"/tmp/bashrc-xtrace.$$".log
export BASH_XTRACEFD=$__XFD
# Prefix each traced command with:
#   +<timestamp> <file>:<line>:
export PS4='+${EPOCHREALTIME} ${BASH_SOURCE}:${LINENO}: '
# Enable tracing and source bashrc
set -x
source ~/.bashrc
set +x

# Close trace FD
exec {__XFD}>&-

Run it with a clean shell:

bash --noprofile --norc -i /tmp/profile-bashrc-xtrace.sh

This produces a log like:

+1702901234.421391 /home/me/.bashrc:1380: stow ...
+1702901234.567842 /home/me/.bashrc:1471: curl ...

Top slowest commands

This analyzer:

Interpreting the results

What showed up at the top:

Things to try to solve them (YMMV):