DesignLinux

Bash For Loop

Loop is the basic requirement of any programming languages. Loops are useful when you want to execute a series of commands until the certain condition is satisfied. In Bash, loops are useful for automating repetitive tasks. In this tutorial, we will see basics of for loop in Bash.

There are three basic loop constructs, for loop, while loop , and until loop. We will also learn how to use the break and continue statements.

The Standard Bash for Loop

The for loop iterates over a list of items and performs the given set of commands.

Following is the form of Bash for loop:

for item in [LIST]
do
  [COMMANDS]
done

In the list items can be series of strings, an array, a range of numbers, output of a command, etc.

Loop over strings

Let’s take an example, there is a list of strings and the variable element will be set to the current item:

for element in Chicago Boston Philadephia Portland
do
  echo "Element: $element"
done

The loop will produce the following output:

Element: Chicago
Element: Boston
Element: Philadephia
Element: Portland

Loop over a number range

To specify a range of numbers or characters by defining start and end point of the range, use the sequence expression. Below is the form for sequence expression:

{START..END}

Below is the example loop which iterates through all numbers from 0 to 5:

for i in {0..5}
do
  echo "Number is : $i"
done
Number is : 0
Number is : 1
Number is : 2
Number is : 3
Number is : 4
Number is : 5

You also can specify the increment when using range. It’s form will be as following:

{START..END..INCREMENT}

Let’s see an example showing how to increment by 2:

for i in {0..10..2}
do
  echo "Number is : $i"
done
Number: 0
Number: 2
Number: 4
Number: 6
Number: 8
Number: 10

Loop over array elements

If the data in array form you can use the for loop to iterate over array elements:

Here, an array named as CITIES and iterating over each element of the array.

CITIES=('Chicago' 'Boston' 'Philadephia' 'Portland')

for city in "${CITIES[@]}"; do
  echo "City: $city"
done
Book: Chicago
Book: Boston
Book: Philadephia
Book: Portland

break and continue Statements

You can control the for loop execution by using the break and continue statements.

break Statement

The break statement is usually used to terminate the loop when a certain condition is satisfied. It terminates the current loop and passes program control to the statement that follows the terminated statement.

Below is an example, in which we are using the if statement to terminate the execution of the loop when the current iterated item is equal to “Chicago”.

for element in Boston Philadephia Chicago Portland; do
  if [[ "$element" == 'Chicago' ]]; then
    break
  fi
  echo "Element: $element"
done

echo 'Done!'
Element: Boston
Element: Philadephia
Done!

continue Statement

The continue statement is used to exit from the current iteration of a loop and go to the next iteration.

In below example given the range of numbers. When the current iterated item is matched with 3, the continue statement will execute to return to the beginning of the loop and to continue with the next iteration.

for i in {1..5}; do
  if [[ "$i" == '3' ]]; then
    continue
  fi
  echo "Number: $i"
done
Number is : 1
Number is : 2
Number is : 4
Number is : 5

Bash for Loop Example

Below is the example to rename the all files of current directory which have space in name. We are replacing space to dash.

for file in *\ *; do
  mv "$file" "${file// /-}"
done

Let’s understand the code line by line:

Conclusion

In Bash, for loops are useful to execute a repetitive commands.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version