BASH Programming

How to Find Ubuntu Version and Codename in a Shell Script

Ubuntu is a popular open-source operating system widely used by developers. It is important to know the version and codename of the Ubuntu installation for various purposes such as troubleshooting, software compatibility, and system upgrades, this article will explain how to find the Ubuntu version and codename in a shell script.

How to Find Ubuntu Codename and Version in a Shell Script

There are several ways to find the Ubuntu version and codename in a shell script and the simplest one  is to use the lsb_release command. This command provides information about the distribution of Linux, including the Ubuntu version and codename and for example, to extract only the Ubuntu version information, one can use:

lsb_release -r

 
Similarly, to find the codename of Ubuntu then use the following command in bash script:

lsb_release -c

 
Now here is the shell script that uses the both above commands to get the version and codename of Ubuntu:

#!/bin/bash
Version=$(lsb_release -r)
echo $version
Codename=$( lsb_release -c)
echo $codename

 
This shell script utilizes the lsb_release command to find the Ubuntu version and codename of the system. The lsb_release command provides information about the Linux Standard Base (LSB) and distribution-specific information.The version variable stores the release version of the Ubuntu distribution and Codename variable stores the codename of the Ubuntu distribution.

To print the values of the version and Codename variables in the terminal, this code uses the echo command. By running this script, the user can quickly and easily determine the version and codename of the Ubuntu distribution currently running on their system.

Conclusion

Knowing the Ubuntu version and codename is essential for maintaining and troubleshooting the system. In this article, we discussed how to find the Ubuntu version and codename in a shell script using the lsb_release command. This method can be used in shell scripts to extract the Ubuntu version and codename information for various purposes.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.