The lsb_release
command was added to the Linux Standard Base (ISO/IEC 23360) for this purpose:
$ lsb_release -si
Ubuntu
$ lsb_release -sd
Ubuntu 18.04.3 LTS
$ lsb_release -sr
18.04
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
Therefore a case statement along the lines of
case "`/usr/bin/lsb_release -si`" in
Ubuntu) echo 'This is Ubuntu Linux' ;;
*) echo 'This is something else' ;;
esac
should do what you want.
On newer Linux distributions based on systemd there is also /etc/os-release, which is intended to be included into shell scripts with the source (.) command, as in
. /etc/os-release
case "$ID" in
ubuntu) echo 'This is Ubuntu Linux' ;;
*) echo 'This is something else' ;;
esac
But in the use-case example you gave, you may actually be more interested not in the name of the distribution, but whether it has apt-get
or yum
. You could just test for the presence of the files /usr/bin/apt-get
or /usr/bin/yum
with if [ -x /usr/bin/apt-get ]; then
... or for the presence of associated infrastructure directories, such as /var/lib/apt
and /etc/apt/
.