Loops are a powerful feature in any programming language. If you do not know already, the loops are a way to repeat the code based on certain criteria.
For example, imagine that you have to print the numbers from 1 to 10. You can write the echo command ten times but that's very primitive. You use a loop and in 3-4 lines of code, it can be done.
That's the simplest of the examples I could think of. I am going to share actual useful examples while I discuss the bash loops with you.
There are three types of loops in Bash:
For
While
Until
I'll show all three kinds of looping in the tutorial. Let's start with the most common one.
For loop in bash
Here's the syntax for 'for loop' in bash:
for arg in LIST; do
commands
done
The LIST here could be an array or a list of items. Brace expansions are also popular for looping.
Take the simplest scenario I mentioned in the beginning. Let's print numbers from 1 to 10 using for loop:
#!/bin/bash
for num in {1..10}; do
echo $num
done
If you run it, you should see an output like this:
You could have also used for num in 1 2 3 4 5 6 7 8 9 10; do but using the brace expansion makes the code look shorter and smarter.
{..} is used for expanding on a pattern. You use {d..h} and it is equivalent to d e f g h . More on brace expansion can be found in this article.
💡
If you are familiar with C programming, you may like using the C-styled for loops in bash:
for ((i = 0 ; i < 10 ; i++)); do
echo $i
done
Let's see another example that displays all the contents of an array in bash:
#!/bin/bash
distros=(Ubuntu Fedora Debian Alpine)
for i in "${distros[@]}"; do
echo $i
done
If you run the script, it will display all the distros defined in the array:
Ubuntu
Fedora
Debian
Alpine
While loop in bash
The while loop tests a condition and then keeps on looping as long as the condition is true.
while [ condition ]; do
commands
done
If you take the previous example, it can be rewritten using the while loop like this:
#!/bin/bash
num=1
while [ $num -le 10 ]; do
echo $num
num=$(($num+1))
done
As you can see, you had to define the variable num to 1 first and then in the loop body, you increase the value of num by 1. The while loop checks the condition and runs it as long as num is less than or equal to 10.
Thus, running the script now will show the exact result you saw earlier with for loop.
#!/bin/bash
echo "Table for $1 is:"
index=1
while [ $index -le 10 ]; do
echo $(($1*$index))
index=$(($index+1))
done
If you are confused about the use of $1, it represents the first argument passed to the script. Check out chapter 3 of this series for more details.
If you run the script, it should show this output:
abhishek@itsfoss:~/bash_scripts$ ./table.sh 2
Table for 2 is:
2
4
6
8
10
12
14
16
18
20
Until loop in bash
This is the lesser-used loop format. It behaves similarly to the while loop. The difference here is that the loop runs until the condition it checks is false. I'll explain it in a bit. Let's see its syntax first.
until [ condition ]; do
commands
done
Now, if I have to use the same example of printing numbers from 1 to 10 using until loop, it would look like this:
#!/bin/bash
num=1
until [ $num -gt 10 ]; do
echo $num
num=$(($num+1))
done
The difference is in the condition; the rest remains the same.
The while loop ran while the variable num was less than or equal to 10.
The until loop runs until the variable num becomes greater than 10.
Both are different ways of doing the same thing. While is more popular as you'll find a while loop equivalent in most programming languages.
🏋️ Exercise time
That was fun. Time to do some exercise now.
Exercise 1: Write a script that takes a number as an argument and prints its table. Your script should also show a message if the script is run without an argument.
Expected output:
$: ./table.sh
You forgot to enter a number
$: ./table.sh 3
3
6
9
12
15
18
21
24
27
30
Exercise 2: Write a script that lists all the files in the directory /var
Hint: Use for loop with /var/* as the 'list'.
You can discuss your answers in this dedicated thread in the Community:
The bash basics series is coming to an end. As the final chapter in the series, you'll learn to use functions in bash scripting next week. Stay tuned.
The Linux terminal could be intimidating. The dark screen with just commands to use. It's easy to feel lost.
The thing is that Linux command line is a vast topic. You can manage the entire system using just the commands. I mean that's the role of sysadmins, network engineers and many other jobs.
The aim of this tutorial collection is not to make you job-ready. It intends to give you the starting point of your Linux command line journey. It will give you enough to navigate the terminal and understand a few basic things like reading files and editing them.
Since it's just the beginning, most of the tutorials are in the 'file operation' category. That's where most Linux books and courses begin.
📋
The best way to learn is by doing it yourself. I have written the tutorials in 'hands-on mode' so you can follow the examples on your Linux system. Each chapter in the series contains some sample exercises to practice your learning. Follow, practice and you'll be getting better at Linux command line in no time.
Chapter 0: Get familiar with the terminal and terminology
When you are absolutely new to the terminal, you'll find yourself lost even while reading the tutorials. That's because you may know always understand even the simplest of terms.
The article below will help you with some of that. While some of the tips I shared may be a little advance or not-so-useful for you at this moment, you'll find plenty of useful stuff.
Chapter 1: Changing directories
In the first chapter, learn to switch directories (folders) using absolute and relative paths. This way, you can navigate in the Linux command line.
Chapter 2: Making directories
Now that you know about switching directories, learn about creating new ones.
Chapter 3: List what's inside a directory
You are getting a good grasp of the directories. Learn to see inside directories and see what files and subdirectories do they have.
Chapter 4: Creating files
Enough about directories. Learn to create new files in the Linux command line.
Chapter 5: Reading files
What's inside the file? Learn to read text files in this chapter.
Chapter 6: Deleting files and directories
Now that you have learned to create new files and folders, it's time to delete them.
Chapter 7: Copying files and directories
Keep on with the file operations and learn to copy files and directories in this installment of the terminal basics series.
Chapter 8: Moving files and directories
Moving file operation is like cut-paste. You can use the same method for renaming files and directories as well.
Chapter 9: Editing files
As the last major file operation, learn to edit text files in the command line.
Chapter 10: Getting help
Now that you have learned plenty of the basic Linux command line operation, it's time to know how you can get help in the terminal itself.
Where to go from here?
Now that you are more comfortable with the terminal and know the basic file operations in the command line, you may wonder what comes next.
I will suggest getting a Linux book like How Linux Works. However, you can start with any Linux book you come across. Here are a few Linux books I love.
Bash scripting is also an integral part of Linux learning. Even if you don't have to write shell scripts, if you know the basics, you should be able to understand scripts you come across while using Linux.
There is no limit to learning. It's impossible to know it all. Yet, if you know at least the basics, it helps you in using the system more effectively.
🗨 I hope you like this Linux terminal tutorial series. Do share your feedback in the comments section.
And when you start adding random external repositories, you’ll encounter update errors sooner or later.
Now, you can search for the error on the internet and perhaps fix it as well. Most of the time, you won’t understand what causes the problem and how it was fixed.
But if you understand the repository mechanism in Ubuntu, you’ll understand the root cause of the problem and fix it accordingly.
You might find all this information in fragments on It’s FOSS and various forums. But the fragmented information is not easy to understand.
Hence, I created this page to give you a comprehensive understanding of how the repository system works.
This guide also benefits the users of Linux Mint, elementary OS and other distributions based on Ubuntu (and perhaps Debian).
📋
This is intended for end users, not developers. I will not show you how to package an application for Ubuntu. You’ll get the necessary knowledge for maintaining your system’s repository sources.
Let's start with the essentials first. Understands the basic concept of the package manager and the repositories. This will build a foundation for you to understand how things work underneath.
Chapter 1: What is a Package Manager?
In simpler words, a package manager is a tool that allows users to install, remove, upgrade, configure and manage software packages on an operating system.
Ubuntu provides the software through repositories. The repositories contain software packages of different kinds. They also have metadata files that contain information about the packages such as the name of the package, version number, description of package and the repository name etc.
The package manager interacts with the metadata and creates a local metadata cache on your system. When you ask it to install a software, the package manager refers to this cache to get the information and then uses the internet to connect to the appropriate repository and downloads the package first before installing it on your system.
This article explains the working of the package manager in a bit more detail. Do check it out.
Chapter 2: Ubuntu’s Default Repositories
From the previous section, you have some idea of the repositories.
You may have guessed that there is not a single repository that contains all the packages. Why not? Because it is categorized to have the packages in different repositories so that they can be enabled or disabled accordingly.
Each Ubuntu version has its own official set of four repositories:
Main – Canonical-supported free and open-source software.
Universe – Community-maintained free and open-source software.
Restricted – Proprietary drivers for devices.
Multiverse – Software restricted by copyright or legal issues.
Chapter 3: Understanding sources.list
The sources.list is an integral part of Ubuntu's repository mechanism. Several update-related issues find their root cause in the incorrect entries in the sources.list file.
This is why it is important to understand it.
Sources.list is basically a text file that contains the repository details. Each uncommented line represents a separate repository.
But these entries follow a specific format and should not have duplicate entries. Read more about them in the article below.
Chapter 3: Going in-depth with PPA
Though Snaps are pushed by Ubuntu a lot these days, PPAs are still a reliable way to get newer software in Ubuntu.
Many Ubuntu users blindly run three commands to use the PPA and naturally end up with issues like 'repository does not have release file'.
This is an in-depth and one-of-a-kind guide on PPA. It will improve your knowledge tremendously.
Adding External Repositories
Apart from the default repositories and PPAs, you will also add software from their party repositories. Softwares like Brave browser, Docker and many more provide dedicated repositories for Ubuntu.
Understand the mechanism of using external repositories.
Troubleshooting Common Errors
Now that you are familiar with the underlying mechanism, it's time to get familiar with the typical update errors you might encounter while using Ubuntu.
When you see go through the error, you may start getting the hint about the root cause. And when you go through the root cause, you will understand how the specific solution fixes it.
And that's the aim of this page. To give you enough under-the-hood knowledge on the package management system so that you can avoid common errors or effectively fix them.
You'll understand the why along with the how.
Failed to Download Repository Information
If you encounter this error, it's just the beginning of your investigation.
While the internet connection could be the issue in some cases, chances are that you have an issue because of some repository you added or wrong entries in the sources.list.
You should jump into the terminal and (try to) refresh the package cache.
sudo apt update
Look at the last few lines of the output. It will give you the error message (lines starting with E:). When you have the error message, your troubleshooting begins.
Repository Does Not Have a Release File
A common error Ubuntu users encounter while blindly adding PPAs to the system without checking if the PPA exists for their Ubuntu version or not.
E: The repository ‘http://ppa.launchpad.net/numix/ppa/ubuntu focal Release’ does not have a Release file.
Let me give you a hint. The repository is not configured for the Ubuntu version being used.
Problem With MergeList
If you see an error like this:
E:Encountered a section with no Package: header,
E:Problem with MergeList /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_precise_universe_binary-i386_Packages,
E:The package lists or status file could not be parsed or opened.
It indicates that somehow the cached file mentioned in the second line got corrupted. The fix is to empty the cache and repopulate it.
Target Packages is configured multiple times
Alright! Technically, not an error. It's just a warning. But it is still quite common and could trouble many new Ubuntu users.
This is a good example for putting your newly acquired knowledge here.
There are duplicate entries in the sources.list files and that's causing the issue. The solution is to delete all the duplicate lines keeping just one.
Continue Learning
I hope you have a slightly better understanding of the repository mechanism in Ubuntu. The next time you encounter an error and come across a solution, you are more likely to understand what's going on.
Next, you should learn to use the apt command for package management. There's more than just apt install or apt remove.
You may also go a bit into detail about package management.
Since Ubuntu is pushing for Snaps more than ever, learn the essential Snap commands.
Flatpak applications are also gaining popularity and you may come across them sooner or later.
💬 I hope you enjoyed learning about the repository mechanism and has more clarity on package management. Do share your thoughts in the comment section.