1. FOR loops & WHILE statements
In this lesson, you will learn to loop in Bash scripts with FOR and WHILE. Let's get started!
2. Basic FOR Loop structure
You may be familiar with using FOR loops from either
Python.
Or R.
3. FOR Loop in Bash
A Bash FOR loop is similar,
The syntax still begins with FOR something in something.
However, there is 'do' before and 'done' after the iterated code.
The output is the same as the R and Python examples.
4. FOR loop number ranges
In Bash, there is a neat shortcut to create a numeric range to iterate through. This is called a 'brace expansion' and uses curly brackets.
Inside curly brackets put the starting number, two periods, the last number, two periods and the increment to increase by. The increment defaults to one if not specified.
Let's create a brace expansion to start at one, finish at five and increase by two.
The iterated code will just echo the looped variable.
Indeed, the numbers one, three, and five are printed out.
5. FOR loop three expression syntax
For numerical loops, Bash also has something called 'three expression syntax'.
This is similar to brace expansion, except with double parentheses.
The first is the start expression, where you assign the variable to increment.
The second part is the terminating condition. That is, when you stop the looping.
The last is the expression for how to change the created variable in the loop.
Our example should assign X to the value of two, continue until X is no longer less than or equal to 4 and increment by two.
The output is as expected; two and four.
6. Glob expansions
You can create iterables in-place using 'glob expansions' or pattern-matching expansions using the star symbol. This will look similar to pattern-matching in terminal.
In this example, pretend there are two text documents in the books directory.
In this FOR loop, we use glob expansion to automatically create an array to iterate over and print out the file name.
Inside the code is just printing out each file in turn.
7. Shell-within-a-shell revisited
Do you remember our handy shell-within-a-shell technique?
You can also use this to create iterables in-place to loop through.
Assume we have the following folder structure with some text and CSV files.
8. Shell-within-a-shell to FOR loop
We could call a shell-within-a-shell and loop through the result.
Here we use ls to list all the files and pipe to a grep pattern-match to only keep those with 'air' in their name (case insensitive).
Inside the FOR loop we just print out each variable.
As expected, we print only the files with 'air' in the filename.
9. WHILE statement syntax
WHILE loops are similar to FOR loops. However, instead of a defined set to loop over, a WHILE loop sets a condition to test each iteration.
Iteration continues until this condition is no longer true.
You start by using the word WHILE instead of FOR.
Then wrap a condition to check at each iteration in square brackets. For numerical conditions, the same comparisons you learned from IF statements can be used.
Multiple conditions can be used just the same as IF statements.
Make sure you make a change inside that eventually triggers the stop condition else you may have an infinite loop!
10. WHILE statement example
Here is a simple WHILE loop example.
The variable x is set to one to start.
The condition in square brackets is to continue while x is less than or equal to three.
At each iteration, the value of x is printed out. Then x is increased by one using the double parenthesis arithmetic method you previously learned about.
As expected, the numbers one to three are printed out.
11. Beware the infinite loop
Beware infinite loops. Without increasing x at each iteration, the condition will never be broken (x will never reach three).
In this example, we comment out the x increment.
This will print out 1 forever!
12. Let's practice!
Let's practice creating FOR and WHILE loops in Bash.