Bash For Loop

In this topic, we will understand the usage of for loop in Bash scripts.

Like any other programming language, bash shell scripting also supports 'for loops' to perform repetitive tasks. It helps us to iterate a particular set of statements over a series of words in a string, or elements in an array. For example, you can either run UNIX command (or task) many times or just read and process the list of commands using a 'for loop'.

Syntax of For Loop

We can apply 'for loop' on bash script in two ways. One way is 'for-in' and another way is the c-style syntax. Following is the syntax of 'for loop' in bash shell scripting:

Or

There are some key points of 'for loop' statement:

  • Each block of 'for loop' in bash starts with 'do' keyword followed by the commands inside the block. The 'for loop' statement is closed by 'done' keyword.
  • The number of time for which a 'for loop' will iterate depends on the declared list variables.
  • The loop will select one item from the list and assign the value on a variable which will be used within the loop.
  • After the execution of commands between 'do' and 'done', the loop goes back to the top and select the next item from the list and repeat the whole process.
  • The list can contain numbers or string etc. separated by spaces.

Some of the 'for loop' examples are given below to illustrate how do they work:

Basic 'For Loop' Example

Bash Script

Output

Bash For Loop

For Loop to Read a Range

Bash Script

Output

Bash For Loop

For Loop to Read a Range with Increment/Decrement

We can increase or decrease a specified value by adding two another dots (..) and the value to step by, e.g., {START..END..INCREMENT}. Check out the example below:

For Increment

Output

Bash For Loop

For Decreament

Output

Bash For Loop

For Loop to Read Array Variables

We can use 'for loop' to iterate the values of an array.

The syntax can be defined as:

Output

For each element in 'array', the statements or set of commands from 'do' till 'done' are executed. Each element could be accessed as 'i' within the loop for the respective iteration. Check out the example below explaining the use of 'for loop' to iterate over elements of an array:

Bash Script

Output

Bash For Loop

For Loop to Read white spaces in String as word separators

The syntax can be defined as below:

Here, str refers to a string.

The statements from 'do' till 'done' are executed for each 'word' of a string. Check out the example below:

Bash Script

Output

Bash For Loop

For Loop to Read each line in String as a word

The syntax can be defined as below:

Here, the statements from 'do' till 'done' are executed for each 'line' of a string. Check out the example below:

Bash Script

Output

Bash For Loop

Note: The only difference between 'For Loop to Read white spaces in String as word separators' and 'For Loop to Read each line in String as a word' is the double quotes around string variable.

For Loop to Read Three-expression

Three expression syntax is the most common syntax of 'for loop'. The first expression refers to the process of initialization, the second expression refers to the termination, and the third expression refers to the increment or decrement.

Check out the example below to print 1 to 10 numbers using three expressions with for loop:

Bash Script

Output

Bash For Loop

For Loop with a Break Statement

A 'break' statement can be used inside 'for' loop to terminate from the loop.

Bash Script

Output

Bash For Loop

For Loop with a Continue Statement

We can use the 'continue' statement inside the 'for' loop to skip any specific statement on a particular condition. It tells Bash to stop executing that particular iteration of the loop and process the next iteration.

Bash Script

Output

Bash For Loop

Infinite Bash For Loop

When there is no 'start, condition, and increment' in the bash three expressions for loop, it becomes an infinite loop. To terminate the infinite loop in Bash, we can press Ctrl+C.

Bash Script

Output

Bash For Loop

Conclusion

In this topic, we discussed how to use for loop statement in Bash to perform specific tasks


Next TopicBash While Loop