Pro Tip: For, While and Until Loops

Image of a terminal prompt

LEVEL: SYSADMIN / TIME: 1-2 min.

Here’s some handy quick reference examples to go back to easily if you are needing to use for, while and until loops in your bash shell scripts. This is perfect for bookmarking and coming back to at a later date. I am always forgetting the subtle syntax differences that bash has, compared to other languages I use, for example C, PHP, Python etc.

Before I show you the examples, it’s worthwhile knowing when to use each type of loop:

  • The for loop is a little bit different from other programming languages. Basically, it let’s you iterate over a series of items within a string.
  • The while loop performs the items to execute if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code.
  • The until loop is almost identical to the while loop, except that the code is executed while the control expression evaluates to false.

FOR Example 1: Simple repetition

This simple type of loop simply counts the values provided. In this case, it starts at 1 and goes to 5. It’s the most basic of repetition.


#!/bin/bash
for i in 1 2 3 4 5
do
echo "Hello, I've done this loop $i times"
done

FOR Example 2: Repeat an action 50 times:

Repetition can be done by starting at a value and incrementing a counter. This is like you’d do in more conventional programming languages. The only thing you have to remember is to use the (( to express a numerical calculation.


#!/bin/bash
for ((i=1;i<=50;i++));
do
# your command, eg: echo $i
done

This example does just what its supposed to do. But there are easier ways that don’t have to make you remember the slightly esoteric double-bracketing. Have a look at the below examples.

RELATED:   Using Google Drive from the Linux Command Line

FOR Example 3: Working with numerical ranges

Ranges

Sometimes you may need to set a step value (for example, count by two’s or to count backwards ). Note that bash version 3.0+ has inbuilt support for using up ranges, but not really practical for step ranges:

#!/bin/bash
for i in {1..5}
do
echo "This is the $i iteration of this loop"
done

Stepping:

Most modern Linux distributions run Bash v4.0+, which has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
echo "This is the $i iteration of this loop"
done

Output:

This is the 0 iteration of this loop
This is the 2 iteration of this loop
This is the 4 iteration of this loop
This is the 6 iteration of this loop
This is the 8 iteration of this loop
This is the 10 iteration of this loop

 

FOR Example 4: Working on words, arguments and the output of commands

Earlier we’ve been working on numerical ranges, but the matter is that the FOR loop evaluates arguments rather than numbers, a number is simply a . This means you can tell it to operate on each line based iteration. For example the following will perform the ls command. For each item it sees it will echo the line. Note that it works on a string of words, not a list of lines. So if you use ls -l, it won’t provide useful output, note also, if you have filenames with spaces in them, it interprets those as different words (arguments that are supplied to for).

RELATED:   Analysing system performance with 'Top'

#!/bin/bash
for i in $( ls *txt ); do
echo item: $i
done

 

WHILE Example:

While essentially emulates the more classic programming language for loop structure:

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

UNTIL Example:


#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done

Related Post

Be the first to comment on "Pro Tip: For, While and Until Loops"

Leave a Reply