2

Suppose I have a set of pre-requisites I need to install for any of a number of linux distributions. In this case, I'd need to switch my install expression against the distribution.

I am aware of uname -v, and I am aware that I can inspect the output of this command with some friable string expression and logic. However what I really need is:

V=$(something-like-uname)
echo $V

"Debian"
# or
"Gentoo"
# or
"Redhat"
# or 
"Arch"
# etc.

What is this command? (Does it exist?)

  • AFAIK there is no posix standard for this so it's going to be different from distribution to distribution. There are plenty of examples out there of code that determines distribution though. – jesse_b Dec 7 '19 at 20:25
  • What about packaging using the various common package systems, like apt and yum, instead of trying to write an installer that does the right thing? Or write a Linuxbrew formula? – Kusalananda Dec 7 '19 at 20:26
2

I'd always check lsb_release -i first and then check /etc/os-release as the fallback. It should be available on many distributions but you can never count on it being everywhere of course. It's Freedesktop standard and is present on systemd systems. On Slackware it shows:

$ cat /etc/os-release
NAME=Slackware
VERSION="14.2"
ID=slackware
VERSION_ID=14.2
PRETTY_NAME="Slackware 14.2"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:slackware:slackware_linux:14.2"
HOME_URL="http://slackware.com/"
SUPPORT_URL="http://www.linuxquestions.org/questions/slackware-14/"
BUG_REPORT_URL="http://www.linuxquestions.org/questions/slackware-14/"

And on Raspbian:

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

The field you're interested here is probably ID.

1

lsb_release -i might do what you want if it is installed. Otherwise the trick it to check for files like /etc/redhat-release, /etc/debian_version, etc.

$ lsb_release -i 
Distributor ID: Fedora
0

I would use:

grep -Po "(?<=^ID=).+" /etc/os-release | sed 's/"//g'
  • This file doesn't exist on my centos machine, or on my debian machine. – jesse_b Dec 7 '19 at 20:26
  • -bash: lsb_release: command not found – jesse_b Dec 7 '19 at 20:28
  • Edited the answer to use os-release that seems to exist on Debian, Ubuntu, and Centos – xenoid Dec 7 '19 at 20:34
  • /etc/os-release doesn't seem to be on my debian machine – jesse_b Dec 7 '19 at 20:35
  • 1
    Yeah my debian is pretty old, debian 6. I think OPs issue is much more complicated than a single command though. – jesse_b Dec 7 '19 at 20:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.