How to: Loop in Bash with Examples

Looping in Bash 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 post describes a loop in Bash with all the necessary examples.

The basic syntax for a loop in Bash is:

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

An example of a loop in Bash:

1: Simple loop:

In the example below for a loop in Bash, we have initialized the variable i. This variable i in a loop from a series of 1 to 5 by incrementing the value of 1. I process the same variable value in a command that echoes the value of i.

	
[[email protected] ~]# cat for_loop_test.sh
for i in 1 2 3 4 5
do
echo "Добро пожаловать на AndreyEx $i раз"
done
[[email protected] ~]# chmod u+x for_loop_test.sh
[[email protected] ~]# ./for_loop_test.sh
Добро пожаловать на AndreyEx 1 раз
Добро пожаловать на AndreyEx 2 раз
Добро пожаловать на AndreyEx 3 раз
Добро пожаловать на AndreyEx 4 раз
Добро пожаловать на AndreyEx 5 раз
[[email protected] ~]#

2. Additional loop in Bash for version 4.0:

In example 1 above, we started the loop by defining and writing all the values ​​of i. But what if your loop has multiple values ​​and you want to iterate over the same. Bash 4.o and higher supports functionality where you can define the start and end values ​​of the series along with the increment value. Lets iterate over from 1 to 12 in increments of 3 and you can define it with the Bash loop example below.

	
[[email protected] ~]# for i in {0..12..3}
> do
> echo "Добро пожаловать AndreyEx $i раз"
> done
Добро пожаловать AndreyEx 0 раз
Добро пожаловать AndreyEx 3 раз
Добро пожаловать AndreyEx 6 раз
Добро пожаловать AndreyEx 9 раз
Добро пожаловать AndreyEx 12 раз
[[email protected] ~]#

3. Reading a directory:

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

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

A sample of the output of a loop in Bash is shown below:

	
File: account
File: cache
File: crash
File: cvs
File: data
File: db
File: ftp
File: ftp_account
File: game
File: lib
File: locale
File: locks
File: log
File: mailru
File: nis
File: opt
File: preserve
File: runs
File: spools
File: temp
File: yp
[[email protected] var]#

4. Syntax of C programming:

You can also use a loop in Bash using the C programming language as a statement as shown below:

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

5. Content of file processing.

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. If you have a file with a list of users and you want to check if they are blocked or not. Then use the example below.

An example of a file with a list of users.

[[email protected] ~]# cat /tmp/users
boris
andreyex
marina
test_ftp_user
testuser
uxuser

In the example below, the variable “i” is initialized with the cat command and passed to the “passwd -s” command, which has an argument as $ i, which is nothing more than a username, to check if he is locked or not.

[[email protected] ~]# for i in `cat /tmp/users`
> do
> passwd -S $i
> done
boris LK 2017-01-13 0 99999 7 -1 (Password locked.)
andreyex LK 2017-01-01 0 99999 7 -1 (Password locked.)
marina 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 multiple hosts

You can also use a loop in Bash, where you can copy files to multiple hosts and you don’t want to type the same command one after the other. In the example below, you will copy the test.txt file to the hosts: “ANDREYEX01 grayex02 manns1 grayex06”, you can also use a loop in Bash to make copying the file a breeze.

for HOST in ANDREYEX01 andreyex02 manns1 andreyex06 
do
scp test.txt $HOST:/tmp/
done

7. LVM mirroring:

You can use a loop in Bash to mirror the LV boolean extension. Suppose you have a vg00 volume group with 10 LV and you want to mirror it with PV sde1. You can then use a loop to run it in a simple, compact solution 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 program using the Bash shell for a multiplication table. In the loop example below, we initialize the variable i, which will be used to multiply a digit using the expr command.

if [ $# -eq 0 ]
then
echo "Пожалуйста введите число неравное 0"
echo "Синтаксис : $0 числа"
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:

[[email protected] ~]# ./myfile.sh 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

Sidebar