How to Check Ubuntu Version Using Bash Scripts? [2 Methods]

If you are working with Ubuntu you may want to know about the version you are currently using. If you know the Ubuntu version it will help you to determine compatibility with certain software or troubleshoot issues specific to a particular release. There are several ways to check the Ubuntu version. In this article, I am going to share some methods of how to check the Ubuntu version using Bash scripts.

Key Takeaways

  • Learning 2 ways i.e. using commands and system files to check the Ubuntu version using Bash scripting.
  • Getting acquainted with commands required to check the Ubuntu version.
  • Handling files i.e. issue file and OS-release file, in Bash scripts to get the current Ubuntu version.

Free Downloads

2 Methods to Check Ubuntu Version Using Bash Scripts

There are several ways to check the Ubuntu version in Bash. You can use the command line to know the Ubuntu version or use a Bash script. There are also different ways that can help you to check the Ubuntu version using Bash scripting. In this section, you are going to learn 2 methods to check the Ubuntu version using Bash scripting, with some practical cases.

You can read our Comparative Analysis of Methods to distinguish between these three methods and pick the best one for your needs.

Method 01: Check Ubuntu Version Using Commands

You can use different commands in your Bash script and then simply run the script when you want to check the Bash version. There are several commands that you can use, including lsb, hostnamectl. You can see the following 3 cases of checking the Ubuntu version using all the commands mentioned here.

Case 01:  Using “lsb” Command in Bash Script to Check Ubuntu Version

You can check the Bash version using the lsb command along with the release option and -a argument. After running the script containing this command, the Bash version will be shown on your device.

Steps to Follow >

➊ First, launch the Ubuntu terminal. You can use the shortcut keys CTRL+ALT+T to do so.

➋ Then create a file with the extension .sh using the nano command and open the script in the text editor like the following.

nano check_version1.sh
EXPLANATION
  • nano: Opens and creates a file in the nano text editor.
  • check_version1.sh: File name.

➌ After that, copy the following script into your text editor.

#!/bin/bash

#Print Bash version
lsb_release -a

EXPLANATION
The code starts with #!/bin/bash, which specifies the Bash interpreter. Then it uses the lsb command along with the release argument and option -a to show the version of Bash.

➍ To save and exit the text editor press CTRL+ O and CTRL+X.

➎ Now, you need to make the bash script file executable. Type the following line in the terminal to do so.

chmod +x check_version1.sh
EXPLANATION
  • chmod: Changes the permissions of files and directories.
  • +x: Argument with the chmod command to add the executable permission.
  • check_version1.sh: File that you want to make executable.

➏ Finally, you can simply run the file from your command line by executing:

./check_version1.sh

checking Ubuntu version using lsb commandIn the image above, you can see that running the script containing the command gives you the Ubuntu version which is in my case Ubuntu 22.04.2 LTS.

Case 02: Using “hostnamectl” Command in Bash Script to Check Ubuntu Version

The hostnamectl command allows users to view and modify the hostname as well as related settings and Ubuntu versions. In this example, I am going to use this command in a Bash script to check the Ubuntu version I am currently using.

You can follow the steps mentioned in case 1 of method 1, to know how to write, execute, and run the bash script.

Script (check_version3.sh) >

#!/bin/bash
#print bash version
hostnamectl

EXPLANATION
Same as the first script, this code starts with #!/bin/bash, which specifies the Bash interpreter. Then it uses the hostnamectl command to show the version of Bash.

Checking Ubuntu version using the command hostnamectlAfter running the Bash script, you can see the Ubuntu version on your computer screen.

Case 03: Using “neofetch” in Bash Script to Check Ubuntu Version

In this case, I am going to show how to use neofetch command to check your Ubuntu version. Neofetch is used to customise terminal displays or display system information in a visually appealing way. Neofetch collects a variety of system data, such as the kernel version, operating system, CPU, GPU, memory, and more, and presents it in a concise and visually appealing ASCII or colored format.

Note: By default, neofetch package may not be installed in your system. Follow this article to install the neofetch command.

You can follow the steps mentioned in case 1 of method 1, to know how to write, execute, and run the bash script.

Script (check_version5.sh) >

#!/bin/bash

#print bash version
neofetch

EXPLANATION
This script starts with #!/bin/bash, which specifies the Bash interpreter. Then the neofetch command will show the Ubuntu version in a formatted and colored way.

Checking ubuntu version using neofetch commandIn this picture, you can see that after running the script, which contains the neofetch command, system information including the Ubuntu version is shown in a formatted, coloured way.

Method 2: Using System Files in Bash Script to Check Ubuntu Version

In this method, you can use system files that already exist in your machine to know the version of Ubuntu. So, here I have demonstrated 2 different cases of opening files using Bash scripts to know the version of Ubuntu I am currently using.

Case 01: Check Ubuntu Version By Opening Issue File

In Linux, the issue file contains information about the operating system release and version. It is typically located in the /etc/ directory. You can write a Bash script that will open this file and show you the version of Ubuntu.

You can follow the steps mentioned in case 1 of method 1, to know how to write, execute, and run the bash script.

Script (check_version2.sh) >

#!/bin/bash

#Print Bash version
cat /etc/issue

EXPLANATION
Same as the other scripts of Bash, this code starts with #!/bin/bash, which specifies the Bash interpreter. Then it displays the information of a file named issue situated in /etc directory,  to show the version of Bash.

Checking Ubuntu version using issue fileAs you can see in this image, after running the script, I got the Ubuntu version printed on my screen.

Case 02: Check Ubuntu Version By Opening Os-release File

The /etc/os-release file in Ubuntu is a vital source of information about the operating system. It provides a variety of information about the Ubuntu distribution, such as the version, codename, and other crucial characteristics.

You can follow the steps mentioned in case 1 of method 1, to know how to write, execute, and run the bash script.

Script (check_version4.sh) >

#!/bin/bash

#Print Bash version
cat /etc/os-release

EXPLANATION
Like a typical Bash script, this script starts with #!/bin/bash, which specifies the Bash interpreter. Then it displays the information of a file named os-release situated in /etc directory,  to show the version of Bash.

Checking ubuntu version using the Os-release fileHere you can see that running the Bash script printed information of the os-release file, including the Ubuntu version.

Comparative Analysis of the Methods

In this section, I am going to give you a comparative analysis of these two methods, mentioned above, so that you can understand which will be best for you to use.

Methods Pros Cons
Method 1
  • You will use familiar commands that you usually use in the command line interface to check the Ubuntu version.
  • You can get coloured and formatted display of the Ubuntu version.
  • You have to remember commands and their associative arguments and options.
  • You may need to install a package to use some commands.
Method 2
  • Don’t have to remember commands or install any package.
  • You must remember the directory of the file.
  • You may require additional permissions in some instances.

If you want to present the version details formally, you should use the neofetch command to do so. Any users can use commands to check the version of Ubuntu, as it doesn’t require any additional permission. On the other hand, users who have knowledge about the specific files and their directories can also use the second method smoothly.

Conclusion

Knowing the Ubuntu version is an easy task, no matter what process you follow. But checking the version using Bash scripting is easier because, even if the version updates, you just have to run a script to check the most recent version. You can do it with just one command in seconds using a bash script. Feel free to let me know if you have further queries on this topic.

People Also Ask

How do I check the Ubuntu version in a shell?
To check the Ubuntu version in the shell you can use the command line interface, and type any of these commands lsb_release -a, cat etc/os-release, or cat /etc/issue.
How do I check the Linux kernel version?

You can use the command uname -r to check the Linux Kernel version that you are using. It will show users the version of Linux kernel as well as some additional information.

How do I check the node version in Ubuntu?

If you want to know the node version in Ubuntu, you must have installed the nodejs in your system. To install it, run the command sudo apt install nodejs, and then run the command node -v.

How do I check the WSL version on Ubuntu?

To check the WSL version, open the PowerShell or Windows Command Prompt. Then enter the command wsl -l -v and this will display the WSL version in Ubuntu.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Lamisa Musharrat

Hello there. My name is Lamisa Musharat, and I'm an Linux Content Developer Executive at SOFTEKO. I earned a bachelor's degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology (BUET).I learned Linux out of my curiosity and now I find it useful as automation is easier using Linux. I take great pleasure in assisting others with Linux-related issues. I really want you to enjoy and benefit from my efforts.Read Full Bio

Leave a Comment