How to use For loop in shell script
How to use For loop in shell script
In this article we will see that How to use for loop in shell script.The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.
#! /bin/bash
#for loops
for VARIABLE in 1 2 3 4 5 ... N
do
command1
command2
commandN
done
for VARIABLE in file1 file2 file3
do
command1 on $VARIABLE
command2
commandN
done
for OUTPUT in $(Linux-or-unix-command-here)
do
command1 on $OUTPUT
command2 on $OUTPUT
commandN
done
for (( EXP1; EXP2; EXP3; ))
do
command1
command2
commandN
done
#Example:
for time in 1 2 3 4 5
do
echo $time
done
command1
command2
commandN
done
#Example:
for time in 1 2 3 4 5
do
echo $time
done
for time in {1..10}
do
echo $time
done
Example:-
for time in {1..10..4}
do
echo $time
done
echo ${BASH_VERSION}
for (( time=0; time<5; time++ ))
do
echo $time
done
#list command with for loops
for command in pwd ls date
do
echo " ...................... $command ...................."
$command
done
#all the word will be consider in for loop
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
done
echo ${BASH_VERSION}
for (( time=0; time<5; time++ ))
do
echo $time
done
#list command with for loops
for command in pwd ls date
do
echo " ...................... $command ...................."
$command
done
#all the word will be consider in for loop
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
0 comments
Please leave your comments...... Thanks