How to use for loops in Linux

Home > Search > How-to
  by

A for loop can be used to loop through a variable or array.

 


Variable

Let's say you have an variable of fruit.

fruit="apple banana orange grape"

 

The following for loop will loop through each piece of fruit in the variable.

#!/bin/bash

fruit="apple banana orange grape"

for myFruit in $fruit
do
  echo $myFruit
done

 

Running the script will produce the following output.

apple
banana
orange
grape

 


Variables with new lines

Let's say you have an variable of fruit where each line is separated by a new line.

fruit="
apples are red
bananas are yellow"

 

By default, the for loop will consider whitespaces, and not newlines, as the delmiter.

#!/bin/bash

fruit="
apples are red
bananas are yellow"

for myFruit in $fruit
do
  echo $myFruit
done

 

In this example, the for loop will produce the following result.

apples
are
red
bananas
are
yellow

 

If you instead want the delimiter to be new lines, add IFS=$'\n' to the script. Once IFS=$'\n' has been added, the for loop will split at new lines.

apples are red
bananas are yellow

 


Array

Let's say you have an array of fruit.

fruit=(apple banana orange grape)

 

The following for loop will loop through each piece of fruit in the array.

#!/bin/bash

fruit=(apple banana orange grape)

for myFruit in ${fruit[@]}
do
  echo $myFruit
done

 

Running the script will produce the following output.

apple
banana
orange
grape

 

 


Loop through directories

Arrays are static collections of data. When working with dynamic data, it is preferrable to create a dynamic. For example, let's say the /opt/servers directory contains directories, such as server01, server02, and server03. You can loop through each server by first creating a variable that contains each server.

servers="$(ls /opt/servers)"

 

You can loop through each server.

#!/bin/bash

servers="$(ls /opt/servers)"

for myServer in $servers
do
  echo $myServer
done

 

 

Running the script will produce the following output. The benefit to this type of approach as that as directories are added or removed from /opt/servers, there is no need to update your script, making the script much more stable and scalable.

server01
server02
server03

 

 


Split at new line

By default, a for loop will split at a white space. For example, let's say you have content such as "Hello World How are you today". By default, a for loop will do the following.

Hello
World
How
are
you
today

 

Setting the IFS (Internal Field Separator) to split at a new line fixes this issue. At the end of your script, unset IFS.

IFS=$'\n'
. . .
your for loop here
. . .
unset IFS

 


Until

In this example, $i is zero, and is incremented by 1, until $i is 7.

for (( i=0; i < 7; i++ ))
do
  echo $i
done

 

Running this script will produce the following.

0
1
2
3
4
5
6

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments