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
do
    Some Commands
done

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};
do
echo $((i*i));
done

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} 
do
 for num in {1..3}
 do
   for Nnum in {1..3}
   do
   echo “First= $num, Second = $Nnum”
done
done

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

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

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

$ bash PrintNum.sh

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.

#!/bin/bash
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++))
do
  echo  "This is your Guess Number $try: "
  read guess
   if [[ $guess == 4 ]]; then
    echo "You guessed my favorite number which is 4"
    break
  fi
done

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

$ bash GuessGame.sh

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.

Conclusion

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.