How to: Bash Loop with Examples

The Bash loop is a very important looping construct in Bash shell programming. Basically a loop in Bash is used to iterate over a series of words within lines. This article describes a loop in Bash with all the necessary examples.

Basic “Bash for Loop” syntax:

for { variable name } in { list }
 
do
 
Command1
 
Command2
 
.
 
.
 
CommmandN
 
done

Bash examples for Loop:

1: Simple loop:

In the example below for the loop, we have initialized a variable. This variable is looped from 1 to 5 by incrementing the value of 1. The same variable value is processed in the command that is echoed to the variable value.

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

2. Additional Bash loop for version 4.0:

In example 1 above, we initiated the loop by writing all the values ​​of the variable, But what if your loop has multiple values ​​and you want to iterate over the same. In Bash 4.o and above, support for functionality where you can define the start and end values ​​of the series along with the increment value. Lets you iterate over from 1 to 20 in steps of up to 4 values, you can define using the example below for a loop.

[[email protected] ~]# for i in {0..20..4}
> do
> echo "Welcome to AndreyEx $i times"
> done
Welcome to AndreyEx 0 times
Welcome to AndreyEx 4 times
Welcome to AndreyEx 8 times
Welcome to AndreyEx 12 times
Welcome to AndreyEx 16 times
Welcome to AndreyEx 20 times
[[email protected] ~]#

3. Reading the directory:

Alternatively, you can initialize Bash for a loop variable with a list of files in a directory using the example below.

[[email protected] var]# for i in $( ls )
do
echo File: $i;
done

An example Bash output for a loop is shown 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
[[email protected] var]#

4. Programming as C syntax:

You can also use Bash for loop using C programming as shown below:

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

5. Processing the content of the file.

You can also use a loop in Bash to process the contents of a file in another command. It helps a lot when processing huge amounts of data. Let’s say you have a file with a list of users and you want to check if they are locked or not. Use below Bash for example.

An example of a file with a list of users.

[[email protected] ~]# cat /tmp/users
mann
foo
bar
test_ftp_user
testuser
uxuser

In the example below, the variable is initialized with the cat command and passed to the “passwd -s” command, which has an argument like $, which is nothing more than a username, to check whether it is blocked or not.

[[email protected] ~]# 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.)
[[email protected] ~]#

6. Copying files to the number of hosts

You can also use bash for a loop where you want to copy files by number of hosts and you don’t want to enter the same command one after the other. In the example below, you want to copy the file test.txt to a file on the host “RHEL01 rhel02 mann1 rhel06”, then you can user loop that 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. Mirroring LVM:

You can use a loop in mirroring the LV boolean extension. Suppose you have a vg00 volume group with 10 LV and you want to mirror it using PV SDE1. Then you can use the below loop to execute it in a simple, compact way, 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 a Bash shell program for the multiplication table. Use below Bash for loop. In the Bash example below for a path, we initialize a variable that we will use to multiply a digit using the 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

Output example:

Sidebar