detecting the os and distro on the system

January 6th, 2010

Linux, in all its diversity, sometimes lacks very basic mechanisms that you would be prepared to take for granted. For instance, picture this scenario.. you are on some system you don't know well. Maybe it's your friend who called you over to his house for help with something. Maybe it's the computer lab where you don't know the setup. Maybe it's a remote session to a box you've never seen. Quick question: what's it running?

Well, bash --version tells you about bash, ls --version tells you about coreutils and so on, you can keep going with that. uname will tell you about the platform and the kernel, useful stuff. What about the distro? Distros are diverse, sometimes it helps a lot to know what kind of environment you're in. Well, strangely enough, there's no standard answer to that.

They all do their own thing, if they do at all. Some distros use lsb_release to dispense this information. Others have files in /etc that you can check for, if you know what they are supposed to be called. So I decided to try and detect this. I've checked a bunch of livecds and it works on those distros that identify themselves somehow.


# Author: Martin Matusiak <>
# Licensed under the GNU Public License, version 3
# <desc> Detect OS (platform and version) of local machine </desc>
# <usage>
# source this file in bash, then run `osdetect`
# </usage>

_concat() {
	local s="$1";shift;
	while [ "$1" ]; do
		s="$s $1"; shift
	echo "$s" | sed "s|^[ \t]*||g" | sed "s|[ \t]*$||g"

_glob() {
	local file=
	local glob=
	local lst=
	while [ -z "$file" ] && [ "$1" ]; do
		lst=$(ls $glob 2>/dev/null | grep -v /etc/lsb-release)
		if [ "$lst" ]; then
			file=$(echo "$lst" | head -n1)
	echo "$file"

osdetect() {
	# ref:

	local os=
	local release=
	local machine=
	if ! which uname &>/dev/null; then
		echo -e "${cred}No uname on system${creset}" >&2
		os=$(uname -s)
		release=$(uname -r)
		machine=$(uname -m)
	if [ "$os" = "SunOS" ]; then
		machine=$(uname -p)
	local platform="$(_concat "$os" "$release" "$machine")"

	# prefer lsb_release
	if which lsb_release &>/dev/null; then
		local id="$(_concat "$(lsb_release -i | sed "s|.*:||g")")"
		local rel="$(_concat "$(lsb_release -r | sed "s|.*:||g")")"
		local code="$(_concat "$(lsb_release -c | sed "s|.*:||g")")"
	elif [ -f /etc/lsb-release ]; then
		local id="$(_concat "$(grep DISTRIB_ID /etc/lsb-release | sed "s|.*=||g")")"
		local rel="$(_concat "$(grep DISTRIB_RELEASE /etc/lsb-release | sed "s|.*=||g")")"
		local code="$(_concat "$(grep DISTRIB_CODENAME /etc/lsb-release | sed "s|.*=||g")")"

	# find a file or another
		local vfile=$(_glob "/etc/*-rel*" "/etc/*_ver*" "/etc/*-version")
		[ "$vfile" ] && local id=$(cat "$vfile")

		# distro specific
		[ "$vfile" = /etc/debian_version ] && [ "$id" ] && id="Debian $id"

	[ "$id" = "n/a" ] && id=
	[ "$rel" = "n/a" ] && rel=
	[ "$code" = "n/a" ] && code=

	local version="$(_concat "$id" "$rel" "$code")"
	[ ! -z "$version" ] && version=" ~ ${cyellow}$version${creset}"

	echo -e "Platform: ${ccyan}${platform}${creset}${version}"

:: random entries in this category ::

2 Responses to "detecting the os and distro on the system"

  1. Jim says:

    Doesn't work on my Gentoo system with lsb-release installed(?)

  2. numerodix says:

    Would you mind giving me something more specific than "doesn't work"?