brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sourcing /etc/os-release gives a Linux script the distro name and version

Linux
~/tips/tip-linux-source-os-release-in-scripts

. /etc/os-release

Let a script check which distribution and release it runs on, without parsing text.

Scripts that need to know the distribution often grep through lsb_release output or /etc/issue. There is a cleaner way: /etc/os-release is written as shell variables, on purpose.

. /etc/os-release
echo "$ID $VERSION_ID $VERSION_CODENAME"

On Ubuntu 24.04 that prints ubuntu 24.04 noble. The os-release man page says the file is designed to be sourced by shell scripts, and lists the fields. ID is a short lowercase name such as debian, ubuntu or fedora. ID_LIKE lists related distributions, which is handy when you want one branch for the whole Debian family:

. /etc/os-release
case " $ID $ID_LIKE " in
  *" debian "*) sudo apt-get install -y curl ;;
  *" fedora "*|*" rhel "*) sudo dnf install -y curl ;;
esac

I prefer this over lsb_release, which is not installed on many minimal systems and containers.

Sourcing the file puts all its variables into your shell, including generic names like NAME and VERSION. In a larger script that can overwrite a variable you already use. Run it in a subshell, for example ID=$( . /etc/os-release; echo "$ID" ), if you only need one value. Some systems keep the real file in /usr/lib/os-release, with /etc/os-release as a link; the man page says to fall back to that path when the first one is missing.

Further reading: os-release(5) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link