The Basics of the Bash “for” Loop
It doesn’t take long before even the newest Linux users learn that the command line is often a quicker, more efficient way to get things done than the GUI. But, sometimes, it may seem that the GUI is a better way to perform redundant task, or a task multiple times, even when you know that there must be a simpler way in the shell. This article will explain how to use the bash “for” loop, which is an invaluable tool that can save you hours of pointing a clicking, or repeating the same command over and over.
The “for” loop is useful when you want to repeat an operation multiple times, for example, on multiple files, or for multiple inputs. The basic syntax of the for loop is as follows; you can type each line into your shell one line at a time:
do
some_command
done
Let’s examine this a little more closely. On the first line, we the “for” statement, which says that, for every item in variable $y, which presumably is a list of items, make the variable x equal to that item in $y. The for loop will then execute all the commands between “do” and “done”, once for each item in $y.
To see what this actually does, you can build a very simple for loop:
do
echo “Hello, world, ${x} times”
done
The output of this will be simply,
Hello, world, 2 times
Hello, world, 3 times
Hello, world, 4 times
Hello, world, 5 times
In reality, this is how it should appear in your terminal:
Not a very useful loop, but now you can see how the basics work.
Once you understand the fundamental structure of the “for” loop, it is easier to build a useful command. For example, let’s say that you want to rename a bunch of files in some predictable manner; perhaps, you want to move all the “.txt” files to “.txt.old”. It would take a very long time to do this with the GUI, and probably just about as long with the command line if you didn’t use a loop. But the for loop makes it trivial:
do
mv ${eachTxtFile} ${eachTxtFile}.old
done
The above snippet will rename all files that are in the current directory, and end with .txt, to their current name, with “.old” appended. Now what would have taken a significant amount of tedious labor in the GUI is done in just a few seconds, because you’ve used a simple for loop.
Sometimes you want to repeat a certain command several times, like in the above “hello world” example, but more times than you’d like to type out. Bash has a built-in function for this, which allows you to specify a range to act on. For example, the above Hello World example can be simplified, using a range:
do
echo “Hello, world, ${x} times”
done
Or, perhaps you want to create a thousand files. This might take all day by hand, but automating it with a for loop can complete the task in just a second:
do
touch $eachNumber
done
These are all very basic loops, but you can probably see how they can quickly become huge time-savers. In my next article, I will discuss the “if” conditional statement, and how to integrate it with the “for” loop, for much more powerful commands.
Like this post?
- Subscribe to the RSS feed to get more like it!
- Share it:
- Leave a comment!
- Related Posts:




Mon, Mar 22, 2010