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?)

Kusalananda
  • 333,661
Chris
  • 961
  • 7
  • 20
  • 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 07 '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 07 '19 at 20:26

3 Answers3

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

I would use:

grep -Po "(?<=^ID=).+" /etc/os-release | sed 's/"//g'
xenoid
  • 8,888
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