1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – For Loop

For Loop in Bash

As any other programming language, shell scripts also support for loops to do the repetitive tasks. Read this tutorial to understand uses of for loop with examples.


  // scope of for loop

for loop executes for each parameter once defined. The VARIABLE is initialized with the parameter’s value which can be accessed in inside the for loop scope. These parameters can be any number, string etc.

#1. Bash – For Loop Example

Check below basic for loop which iterates 5 times.

You can also define a range with for loop in the bash script with numeric values.

The arguments can be also a string like:

#2. Bash – For Loop in C Style

You can also write for loop in bash script similar to for loop in c programming. For example to print 1 to 10 numbers.

#3. Bash – For Loop with Files

You can access filenames one by one in for loop under the specified directory. For example, read all files from the current directory.

The above loop will iterate the number of times as the number of files available. It will select one by file in each iteration.

Tags , , , ,
Was this article helpful to you? Yes 2 No