Speeding up my bashrc

26 July 2025


Recently I’ve been having long startup times when booting up a terminal instance on my laptop. I traced it to a line in my .bashrc. I first assumed it was the lines initialising my mamba environment but I timed this and it was fast.

I ran time pfetch and noticed it took 3 seconds to run to completion.

05:27:37 sam@localhost pfetch-caching → time pfetch 
  _____   ______       sam@localhost.localdomain
 / ____\ / ____ \      os     openSUSE Tumbleweed
/ /    `/ /    \ \     host   2325VZ9 ThinkPad X230
\ \____/ /,____/ /     kernel 6.15.4-1-default
 \______/ \_____/      uptime 13m
                       pkgs   5496
                       memory 4381M / 7643M


real    0m3.177s
user    0m3.076s
sys     0m0.152s

Almost all of this time is due to querying the number of packages on my system with rpm -qa.

05:39:06 sam@localhost ~ → time rpm -qa | wc -l
5496

real    0m3.249s
user    0m3.159s
sys     0m0.138s
05:39:11 sam@localhost ~ → 

I need pfetch in my bashrc (everyone else has it so I need it too) so removing it isn’t an option.

I decided to cache the result of pfetch with a cron job that runs every minute. I initially defined my job like this

* * * * * pfetch > ~/.pfetch-output

But then I noticed that the .pfetch-output file it generated was empty. I guessed it was due to it not finding pfetch so ran which pfetch to find where it was located and give cron the absolute path. When I did this I was surprised by receiving “mail” in /var/mail/sam

05:25:33 sam@localhost pfetch-caching → which pfetch
/home/sam/.local/bin/pfetch
You have mail in /var/mail/sam

I didn’t actually know I could receive mail like this

05:25:35 sam@localhost pfetch-caching → cat /var/mail/sam 
From sam@localhost  Sat Jul 26 05:25:01 2025
Return-Path: <sam@localhost>
X-Original-To: sam
Delivered-To: sam@localhost
Received: by localhost (Postfix, from userid 1000)
        id 37EC92AF2CC; Sat, 26 Jul 2025 05:25:01 +0100 (BST)
From: "(Cron Daemon)" <sam@localhost>
To: sam@localhost
Subject: Cron <sam@localhost> pfetch > .pfetch-output
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <LANG=en_GB.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/sam>
X-Cron-Env: <PATH=/usr/bin:/bin:/usr/sbin:/sbin>
X-Cron-Env: <LOGNAME=sam>
X-Cron-Env: <USER=sam>
Message-Id: <20250726042501.37EC92AF2CC@localhost>
Date: Sat, 26 Jul 2025 05:25:01 +0100 (BST)

/bin/sh: line 1: pfetch: command not found

Which is what I expected the issue to be and replacing pfetch with /home/sam/.local/bin/pfetch produces the right output. I replaced the pfetch call in my bashrc with cat .pfetch-output and my bashrc takes less than a year to run which is quite nice.

Actually, since the only issue with pfetch was finding the number of packages on my system, I decided to cache the package list to a file and modify pfetch so that it read from this file.

05:51:54 sam@localhost pfetch-caching → crontab -l
* * * * * rpm -qa > .rpm-qa-output
05:51:56 sam@localhost pfetch-caching →

This has the added bonus that when pfetch runs I have an up-to-date memory usage. As an aside, I learned how to do line-profiling of bash scripts which I find kinda funny and feels pretty extra and unecessary.

To be honest, I find this entire fiasco a bit funny.

References

[1]: https://bauer.codes/post/2025/04/bash-profiling/