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 sh, you may want to know about your current versions you examples 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.
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.
➊ 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
- 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
➍ 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
- 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
In 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.
Script (check_version3.sh) >
#!/bin/bash
#print bash version
hostnamectl
After 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.
Script (check_version5.sh) >
#!/bin/bash
#print bash version
neofetch
In 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.
Script (check_version2.sh) >
#!/bin/bash
#Print Bash version
cat /etc/issue
As 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.
Script (check_version4.sh) >
#!/bin/bash
#Print Bash version
cat /etc/os-release
Here 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 |
|
|
Method 2 |
|
|
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
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial