Pro Tip: For, While and Until Loops

Image of a terminal prompt


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.

for i in 1 2 3 4 5
echo "Hello, I've done this loop $i times"

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.

for ((i=1;i<=50;i++));
# your command, eg: echo $i

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.

FOR Example 3: Working with numerical 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:

for i in {1..5}
echo "This is the $i iteration of this loop"


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

echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
echo "This is the $i iteration of this loop"


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).

for i in $( ls *txt ); do
echo item: $i


WHILE Example:

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

while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER

UNTIL Example:

until [ $COUNTER -lt 10 ]; do
let COUNTER-=1

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.