Bash: Getting the systemd version and comparing it

This is just a quick post, but I wanted to spare both future me, and anyone else, from struggling with the precise syntax to extract the systemd version in a bash script and applying some work-around on older versions.

I recently had to work around a bug in systemd (specifically, and needed to modify a service file if the systemd version was less than 241.

To do this, you can extract the version using systemctl --version, pipe it to a fun sed pattern, then use it:


systemdver=$(systemctl --version | sed -nE "s/systemd ([0-9]+).*/\1/p")

# If the systemd version is prior to 241, do something
if [[ $systemdver -lt 241 ]];
   echo 'do whatever'

Replace 241 with which ever version you need to compare against.

That’s it, hope this helps someone.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s