Sample Header Ad - 728x90

How can I get distribution name and version number in a simple shell script?

256 votes
21 answers
358826 views
I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the distribution the script is running (in order to trigger specific actions, for instance the creation of repositories). So far what I've got is this: OS=$(awk '/DISTRIB_ID=/' /etc/*-release | sed 's/DISTRIB_ID=//' | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/') VERSION=$(awk '/DISTRIB_RELEASE=/' /etc/*-release | sed 's/DISTRIB_RELEASE=//' | sed 's/[.]0/./') if [ -z "$OS" ]; then OS=$(awk '{print $1}' /etc/*-release | tr '[:upper:]' '[:lower:]') fi if [ -z "$VERSION" ]; then VERSION=$(awk '{print $3}' /etc/*-release) fi echo $OS echo $ARCH echo $VERSION This *seems* to work, returning ubuntu or centos (I haven't tried others) as the release name. However, I have a feeling that there must be an easier, more reliable way of finding this out -- is that true? It doesn't work for RedHat. /etc/redhat-release contains : Redhat Linux Entreprise release 5.5 So, the version is not the third word, you'd better use : OS_MAJOR_VERSION=sed -rn 's/.*([0-9])\.[0-9].*/\1/p' /etc/redhat-release OS_MINOR_VERSION=sed -rn 's/.*[0-9].([0-9]).*/\1/p' /etc/redhat-release echo "RedHat/CentOS $OS_MAJOR_VERSION.$OS_MINOR_VERSION"
Asked by Alix Axel (2969 rep)
Jan 24, 2011, 05:00 AM
Last activity: Apr 4, 2025, 02:42 AM