Master the Bash For Loop: A Beginner’s Guide

The for loop is a fundamental construct in bash scripting that allows for iterating through a set of values and performing a set of commands for each value. Whether you are automating a task or creating a script to perform a specific function, understanding how to use the for loop is essential. We’ll walk through the basics of the for-loop syntax and provide examples to help you apply this powerful tool in your own scripts.

After going through this detailed guide on Bash for loop, users will understand the following aspects.

Syntax of Bash for loop

If you’ve done coding in any (almost) programming language, then you would be familiar with its syntax, which is the same in bash scripting.

for Number in ITEM1 ITEM2 ITEM3
    Some Commands

Here’s how the loop works:

  • The loop starts with the first item (ITEM1) and assigns it to the variable “Number”.
  • The commands between “do” and “done” are executed using the value of ITEM1
  • After the commands are executed, the loop proceeds to the next item (ITEM2) and assigns it to the variable “Number”.
  • Again, the commands between “do” and “done” are executed using the value of ITEM2
  • This process continues until all the items in the list have been processed.

Note: To run the scripts, follow this article.

Examples of Bash for Loop

Here are some examples of Bash for loops that are being executed directly into the shell and will help you understand better.

Example 1: Calculating a Square For a Range of Numbers

Here’s an example where the iteration of squares of numbers is done from one to ten.

for i in {1..10};
echo $((i*i));

To execute the above-written script, press the “Enter” key after pasting it into the terminal.

The above “for loop” prints the squares of numbers from one to ten, a basic example.

Example 2: Nested for Loop

In this example, there are two lists; in each iteration of the first list, a number ranging from one to three will be printed.

for num in {1..3} 
 for num in {1..3}
   for Nnum in {1..3}
   echo “First= $num, Second = $Nnum”

To execute the above-written script, press the “Enter” key after pasting it into the terminal.

As seen in the above image, the first iteration prints the same number in all lines while the second iteration, which is nested for loop, prints the iterated numbers in each iteration.

Example 3: An Infinite for Loop

Let’s create an infinite for-loop example where the loop can only be terminated when the memory runs out, or you press “Ctrl +C.”

current = 1
echo “You need to press CTRL+C if you want to terminate the loop”
for (( ; ; ))
echo “This is $current iteration”
sleep 2

The above script named “” can be created using any text editor and can be executed using this command in the terminal.

$ bash

The above script will keep running until the system runs out of memory or you press CTRL+C.

Example 4: Guess a Number

In this example, there will be four tries for each run which will guess the favorite number. Here’s the script of it.

random=`shuf -i 0-10`
echo "Each user will be given 4 tries to guess my favorite number between 0 and 8"

for ((try=1; try<=4; try++))
  echo  "This is your Guess Number $try: "
  read guess
   if [[ $guess == 4 ]]; then
    echo "You guessed my favorite number which is 4"

The above script is named “, “and is executed using this bash command.

$ bash

The above script prompts the user to guess the favorite number of the person who wrote the script. It will give the user 4 tries to guess the number. If the user guesses the number correctly on any of the tries, the script will print a message saying that the user has guessed the correct number and will exit the loop. If the user does not guess the correct number within the 4 tries, the loop will end, and the script will terminate.


Writing a code or script with many repetitive actions could make it look bad and hard to understand, so we have a for loop that can also be used in bash scripting. This guide illustrates the use of bash for loop with the help of examples.