Linux
Trending

Bash For Loop Examples

 

How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.
Bash for loop examples for Macos/Linux and Unix

 

 

for loop syntax

Numeric ranges for syntax is as follows:

OR

OR

Examples

This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

Sample outputs:

The seq command (outdated)

WARNING! The seq command print a sequence of numbers and it is here due to historical reasons. The following examples is only recommend for older bash version. All users (bash v3.x+) are recommended to use the above syntax.

The seq command can be used as follows. A representative example in seq is as follows:

There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq. The builtin command are fast.

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).

A representative three-expression example in bash as follows:

Sample output:

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:

Conditional exit with break

You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:

Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.

Early continuation with continue statement

To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.

This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.

Check out related media

This tutorial is also available in a quick video format. The video shows some additional and practical examples such as converting all flac music files to mp3 format, all avi files to mp4 video format, unzipping multiple zip files or tar balls, gathering uptime information from multiple Linux/Unix servers, detecting remote web-server using domain names and much more.



Video 01: 15 Bash For Loop Examples for Linux / Unix / OS X Shell Scripting

Conclusion

You learned how to use the bash for loop with various example. See the following resources for more info.

  • See all sample for loop shell script in our bash shell directory
  • Bash for loop syntax and usage page from the Linux shell scripting wiki
  • man bash
  • help for
  • help {
  • help break
  • help continue

Facebook Comments

Tags
Show More

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button