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

To get OS and VER, the latest standard seems to be /etc/os-release. Before that, there was lsb_release and /etc/lsb-release. Before that, you had to look for different files for each distribution.

Here's what I'd suggest

if [ -f /etc/os-release ]; then
    # freedesktop.org and systemd
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
    # linuxbase.org
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
    # For some versions of Debian/Ubuntu without lsb_release command
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    # Older Debian/Ubuntu/etc.
    OS=Debian
    VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
    # Older SuSE/etc.
    ...
elif [ -f /etc/redhat-release ]; then
    # Older Red Hat, CentOS, etc.
    ...
else
    # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
    OS=$(uname -s)
    VER=$(uname -r)
fi

I think uname to get ARCH is still the best way. But the example you gave obviously only handles Intel systems. I'd either call it BITS like this:

case $(uname -m) in
x86_64)
    BITS=64
    ;;
i*86)
    BITS=32
    ;;
*)
    BITS=?
    ;;
esac

Or change ARCH to be the more common, yet unambiguous versions: x86 and x64 or similar:

case $(uname -m) in
x86_64)
    ARCH=x64  # or AMD64 or Intel64 or whatever
    ;;
i*86)
    ARCH=x86  # or IA32 or Intel32 or whatever
    ;;
*)
    # leave ARCH as-is
    ;;
esac

but of course that's up to you.


I'd go with this as a first step:

ls /etc/*release

Gentoo, RedHat, Arch & SuSE have a file called e.g. /etc/gentoo-release. Seems to be popular, check this site about release-files.

Debian & Ubuntu should have a /etc/lsb-release which contains release info also, and will show up with the previous command.

Another quick one is uname -rv. If the kernel installed is the stock distro kernel, you'll usually sometimes find the name in there.


lsb_release -a. Works on Debian and I guess Ubuntu, but I'm not sure about the rest. Normally it should exist in all GNU/Linux distributions since it is LSB (Linux Standard Base) related.