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 <numerodix@gmail.com> # 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 done echo "$s" | sed "s|^[ \\t]*||g" | sed "s|[ \\t]*$||g" } _glob() { local file= local glob= local lst= while [ -z "$file" ] && [ "$1" ]; do glob="$1";shift; lst=$(ls $glob 2>/dev/null | grep -v /etc/lsb-release) if [ "$lst" ]; then file=$(echo "$lst" | head -n1) fi done echo "$file" } osdetect() { # ref: http://linuxmafia.com/faq/Admin/release-files.html local os= local release= local machine= if ! which uname &>/dev/null; then echo -e "${cred}No uname on system${creset}" >&2 os="N/A" else os=$(uname -s) release=$(uname -r) machine=$(uname -m) fi if [ "$os" = "SunOS" ]; then os="Solaris" machine=$(uname -p) fi 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 else local vfile=$(_glob "/etc/*-rel*" "/etc/*_ver*" "/etc/*-version") [ "$vfile" ] && local id=$(cat "$vfile") # distro specific [ "$vfile" = /etc/debian_version ] && [ "$id" ] && id="Debian $id" fi [ "$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}" }
Download this code: osdetect.sh

Doesn’t work on my Gentoo system with lsb-release installed(?)
Would you mind giving me something more specific than “doesn’t work”?