April 10, 2009

Getting Linux distro name and version

Filed under: linux — SiKing @ 9:56 am

It appears that I am not the only one that would like a consistent way of getting the Linux distribution name and version number for any and all distros. And it appears there is no solution to this dilemma. The following is my attempt at such a solution.

The script will currently give you correct results for only about 60% of ditros out there. Anyone want to add the algorithm for their fav distro, please do so in the comments. I will try to maintain this up-to-date.


if [ -e /etc/*-version ]; then
	if [ $(egrep -q '[[:alpha:]]' /etc/*-version) ]; then
		# the file contains only the version, not the name
		pushd /etc &> /dev/null
		name=$(ls *-version)
		popd &> /dev/null
		printf ${name%-version}
	cat /etc/*-version

elif [ -e /etc/lsb-release* ]; then
	# This is probably the most reliable method, if it is available.
	description=$(lsb_release -d)
	echo ${description#Description:}

elif [ -e /etc/issue ]; then
	# This is quite unreliable, as admins are encouraged to edit this file to their heart's desire.
	head -1 /etc/issue | sed {'s/ [@\][[:alpha:]]//g'}



  1. Generalized the script for any *-version. I think this should be able to hit 60% of distros out there?

    Comment by siking — September 20, 2009 @ 11:59 am | Reply

  2. On OpenSUSE ‘lsb_release -d’ returns:
    Description: openSUSE 11.1 (x86_64)
    It’s taken from the /etc/issue file. I think I’m going to have to find out what the formatting of that file is.
    As for the /etc/*-version, I think I am going to have to do it a little more global-like.

    Comment by siking — June 3, 2009 @ 7:25 am | Reply

  3. elif [ -e /etc/slackware-version ]; then
    cat /etc/slackware-version

    Comment by Terry Sturtevant — May 28, 2009 @ 7:45 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: