Monday, May 22, 2017
Bash for Loop Syntax
Bash for Loop Syntax
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.
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.
for loop syntax
Numeric ranges for syntax is as follows:
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
Examples:
#!/bin/bah
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Using seq command
#!/bin/bash
for i in $(seq 1 2 20)
do
echo "Welcome $i times"
done
To know more about seq command check manual using man seq command
Bash 3.0+ version supports following syntax also
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
Three expression syntax
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
Example:
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..." done
Available link for download