X
    Categories: Linux

Practical Guide: Bash for Loop with examples

Bash for loop is very first and important looping constructs in bash shell programming. Basically bash for loop is used for iterating over series of words within string.This Post describes Bash for loop with all necessary examples.

Basic syntax of “Bash for loop”:

for { variable name } in { list }

do

Command1

Command2 

.

.

CommmandN

done

Examples Bash for Loop:

1:  Simple for loop:

In the below example of Bash for loop we have initiated variable i. This i variable is looped from series 1 to 5 with increment value 1. Same value of i variable is processed in the command which echo’s the value of i variable.

[root@rhel1 ~]# cat for_loop_test.sh
for i in 1 2 3 4 5
do
echo "Welcome to UxTechno $i times"
done
[root@rhel1 ~]# chmod u+x for_loop_test.sh
[root@rhel1 ~]# ./for_loop_test.sh
Welcome to UxTechno 1 times
Welcome to UxTechno 2 times
Welcome to UxTechno 3 times
Welcome to UxTechno 4 times
Welcome to UxTechno 5 times
[root@rhel1 ~]#

 

2.  Advanced loop for Bash 4.0 version:

In the above example 1 we have initiated loop by defining or writing all the values of i variable, But what if your loop has more values and you want to iterate the same. Well Bash version 4.o above support below functionality in which you can define the series start and end values along with increment value. Lets you want to iterate through 1 to 20 with increment value 4 then you can define it using below example of bash for loop.

[root@rhel1 ~]# for i in {0..20..4}
> do
> echo "Welcome to UxTechno $i times"
> done
Welcome to UxTechno 0 times
Welcome to UxTechno 4 times
Welcome to UxTechno 8 times
Welcome to UxTechno 12 times
Welcome to UxTechno 16 times
Welcome to UxTechno 20 times
[root@rhel1 ~]#

 

3. Directory Listing:

You can also initialized bash for loop variable with the list of files in directory using below example.

[root@rhel1 var]# for i in $( ls )
do
echo File: $i;
done

Sample output of Bash for loop is as below:

File: account
File: cache
File: crash
File: cvs
File: db
File: empty
File: ftp
File: ftp_dump
File: games
File: lib
File: local
File: lock
File: log
File: mail
File: nis
File: opt
File: preserve
File: run
File: spool
File: tmp
File: yp
[root@rhel1 var]#

 

4. C programming Like syntax:

You can also use bash for loop using c programming like statement as below:

for ((i=1;i<=25;i=i+1))
do
echo $i
done

 

5. Processing contents of file.

You can also use bash for loop in processing the contents of file within another command. This helps very much in processing huge amount of data.Let’s say you have file with list of users and and you want to check whether they are locked or not. Then use below bash for loop example.

Sample File with list of users.

[root@rhel1 ~]# cat /tmp/users
mann
foo
bar
test_ftp_user
testuser
uxuser

In the below example “i” variable is initialized using cat command and passed to command “passwd -S” having argument as $i which is nothing but the username to check whether its locked or not.

[root@rhel1 ~]# for i in `cat /tmp/users`
> do
> passwd -S $i
> done
mann LK 2017-01-13 0 99999 7 -1 (Password locked.)
foo LK 2017-01-01 0 99999 7 -1 (Password locked.)
bar LK 2017-01-01 0 99999 7 -1 (Password locked.)
test_ftp_user PS 2017-01-07 0 99999 7 -1 (Password set, SHA512 crypt.)
testuser PS 2017-01-01 0 99999 7 -1 (Password set, SHA512 crypt.)
uxuser LK 2017-01-08 0 99999 7 -1 (Password locked.)
[root@rhel1 ~]#

 

6. Copying files on number of Hosts

You can also use the bash for loop where you want to copy files on number of hosts and you don’t want to enter same command one after another. In the below example you want to copy file test.txt file on the host “RHEL01 rhel02 mann1 rhel06” then you can user bash for loop which will allow you to copy the file in one go.

for HOST in RHEL01 rhel02 mann1 rhel06 
do 
scp test.txt $HOST:/tmp/ 
done

 

7. LVM Mirroring:

You can use bash for loop in mirroring the logical extends LV. Let say you have volume group vg00 with 10 LV’s and you want to mirror it using the PV sde1. Then you can use below bash for loop to accomplish it in simple n compact way that to in one go.

for i in 1 2 3 4 5 6 7 8 9 10
do
lvextend -m 1 /dev/vg00/lvol$i /dev/sde1
done

 

8. Multiplication Table:

Let’s say you want to write the bash shell program for multiplication table. Then use below bash for loop. In the below bash for loop example we have initialized variable i which will be used for multiplying the digit using expr command.

if [ $# -eq 0 ]
then
echo "Please do not enter Zero"
echo "Syntax : $0 number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i = `expr $i \* $n`"
done

Sample output:

Related Post