How to find Ubuntu version and codename in a shell script

Question: I am writing a shell script where I need to obtain the release version and release name (or codename) of Ubuntu system. How can I get Ubuntu version/release name inside a shell script?

There are many cases where you need to run some batch processing jobs which can vary across different Ubuntu releases (e.g., Quantal Quetzal, Raring Ringtail, Saucy Salamander, Trusty Tahr, Utopic Unicorn). For example, you may want to download a source package which is available as multiple Ubuntu release versions, or you may want to access Ubuntu repositories for different releases. Often times, available source packages or repositories may be named after the "codename" or "nickname" (e.g., quantal, raring, saucy, trusty, utopic) of Ubuntu releases.

If you need to find the version/codename of your Ubuntu system from the command line, you can use lsb_release command which displays distribution-specific information.

The following shell script snippet demonstrate how to store the version and codename of Ubuntu in two different variables.

#!/bin/sh

version=`lsb_release --release | cut -f2`
codename=`lsb_release --codename | cut -f2`

echo $version
echo $codename

lsb_release command also works on other Linux distros, including Debian, CentOS, Fedora and OpenSUSE.

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Leave a comment

Your email address will not be published. Required fields are marked *