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/
.
<—
This is not the case at all. Sylvain Pineau, for example, exhibited a trivial one-liner reliably querying the current platform type by deferring to Python – which comes preinstalled on most platforms (including Ubuntu). Shell scripts should never attempt to manually query platform metadata with low-level, non-portable tests of the sort implemented in this answer. – Cecil Curry Aug 17 '17 at 03:29awk
ormawk
or ay other implementation would work just as well. – terdon Apr 20 '18 at 08:05lsb_release
:lsb_release -d | awk -F"\t" '{print $2}' | awk -F " " '{print $1}'
– NerdOfCode Nov 04 '18 at 14:27lsb_release -d | awk '{print $3}'
orgrep -oP 'VERSION="\K\S+' /etc/os-release
, no need for the double pipe to awk. – terdon Nov 05 '18 at 09:21