A A
RSS

The Basics of the Bash “for” Loop

Mon, Mar 22, 2010

Tweet this!

Introduction

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.

Basic Syntax

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:

for x in $y
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:

for x in 1 2 3 4 5
do
    echo “Hello, world, ${x} times”
done

The output of this will be simply,

Hello, world, 1 times
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:

A basic "for" loop in bash.

Not a very useful loop, but now you can see how the basics work.

A more useful loop

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:

for eachTxtFile in *.txt
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.

For loop ranges

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:

for eachNumber in {1..5}
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:

for eachNumber in {1..1000}
do
    touch $eachNumber
done
Conclusion

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?

Leave a Reply

Subscribe

Weekly Poll

What do you think of Google Chrome?

View Results

Loading ... Loading ...

Advertisement

Search TechThrob