Bash for loop Examples

There are many times in a shell script when you want to repeatedly execute a statement or group of statements until some specified condition occurs. In addition, you might want to execute a group of statements for each element in a certain list. The shell provides three looping constructs for these situations: the for, while, and until constructs.

The for loop allows you to specify a list of values. A group of statements is executed for each value in the list.

For loop syntax

The for loop in the shell takes a list of words (strings) as an argument. The number of words in the list determines the number of times the statements in the for loop are executed. The syntax for the for loop is:

for variable in element1 element2 element3 
do
 commands 
done

where:

  • variable: It is any variable name.
  • elements (argument_list): It can be any list of words, strings, or numbers, and it can be literal or generated by using a shell command or shell-command-line metacharacter.
  • commands: The commands can be any operating-system commands, a user program, a shell script, or a shell statement that returns (produces) a list.

The value of the variable var is set to the first word in the list the first time through the loop. The second time through the loop, its value is set to the second word in the list, and so on. The loop terminates when var has taken on each of the values from the argument list, in turn, and there are no remaining arguments.

The for Loop Argument List

The argument list in a for loop can be any list of words, strings, or numbers. Generate the argument list using any of the following methods (or combination of methods):

  • Use an explicit list
  • Use the contents of a variable
  • Use command-line arguments
  • Use command substitution
  • Use file names in command substitution

Example 1 : For loop using an Explicit List to Specify Arguments

When arguments are listed for a for loop, they are called an explicit list. An explicit list is the simplest form of an argument list. The loop is executed once for each list element. The following example is a for loop using an explicit list of elements:

#!/bin/bash
for fruit in apple orange banana peach kiwi 
do
    echo "Value of fruit is: $fruit"
done

The output of this for loop is:

Value of fruit is: apple 
Value of fruit is: orange 
Value of fruit is: banana 
Value of fruit is: peach 
Value of fruit is: kiwi

Example 2 : For loop using Variable Contents to Specify Arguments

When the arguments are in a variable and the statement is executed, the variable contents are substituted. In the following example, the text entered at the prompt becomes the value of the variable INPUT. This variable represents the argument list of the for construct. Therefore, the text in INPUT is broken into words or tokens based on white space.

$ cat ex1.sh 
#!/bin/bash
# Script name: ex1.sh
echo "Enter some text: \c" 
read INPUT
for var in $INPUT 
do
 echo "var contains: $var"
done
$ ./ex1.sh
Enter some text: I like the Bash shell. 
var contains: I
var contains: like
var contains: the
var contains: Korn
var contains: shell.

Example 3 : For loop using Command-Line Arguments to Specify Arguments

In the following example, the text entered on the command line becomes the argument list of the for construct. Therefore, the command line text is broken into words or tokens based on white space.

$ cat ex2.sh
#!/bin/bash
# Script name: ex2.sh
for var in $* 
do
 echo "command line contains: $var"
done
$ ./ex2.sh I am The Geek.
command line contains: I
command line contains: am
command line contains: The
command line contains: Geek.

Example 4 : For loop Using Command Substitution to Specify Arguments

The syntax for command substitution in a Bash shell is:

for var in `cmd_sub`

The following example uses the output of the cat command as the argument list.

$ cat fruit1 
apple 
orange 
banana 
peach
kiwi
$ cat ex4.sh 
#!/bin/bash
# Script name: ex4.sh
for var in `cat fruit2` 
do
 echo "var contains: $var"
done
$ ./ex4.sh
var contains: Apple 
var contains: Orange 
var contains: Banana 
var contains: Peach 
var contains: Kiwi

If the file containing the list was in the following format, what is the result of the for loop.

$ cat fruit3
Apple Orange Banana Peach Kiwi
$ cat ex5.sh 
#!/bin/bash
# Script name: ex5.sh
for var in `cat fruit3` 
do
 echo "$var"
done
$ ./ex5.sh
Apple 
Orange 
Banana 
Peach 
Kiwi

In the above example, the results do not change. The IFS variable is any white space, so both a carriage return and space or tab separate each field in the file.

Example 5 : For loop using File Names in Command Substitution to Specify Arguments

Some commands provide file names and directory names as their output. In the following example, the shell substitutes the output of the command, ls /etc/p*(/etc/passwd/etc/profileand so on), as the argument list for the for loop.

$ cat ex7.sh 
#!/bin/bash
# Script name: ex7.sh
for var in `ls /etc/p*`
do
 print "var contains: $var"
done
# ./x.sh 
var contains: /etc/passwd
var contains: /etc/passwd-
var contains: /etc/printcap
var contains: /etc/profile
....
Related Post