Magazine

Looping with the for Command in Linux Bash Script

Posted on the 16 April 2021 by Satish Kumar @satish_kumar86

For iterative operations, thebashshellusesthree types of loops:for,while, anduntil. Using theforlooping command, we can execute a set of commands for afinitenumber of times for every item in a list. In theforloop command, the user-defined variable is specified. After theincommand, the keyword list of values can be specified. The user-defined variable will get the value from that list, and all statements betweendoanddoneget executed until it reaches the end of the list.

The purpose of theforloop is to process a list of elements. It has the following syntax:

for variable in element1 element2 element3 
do 
commands 
done 

The simple script with the for loop could be as follows:

The simple script with the for loop could be as follows:

The simple script with the for loop could be as follows:

for command in clear date cal 
do 
  sleep 1 
  $command 
Done 

In the preceding script, the commandsclear,date, andcalwill be called one after another. Thesleepcommand will be called before every command for one second.

If we need to loop continuously or infinitely, then the following is the syntax:

for ((;;)) 
do 
    command 
done 

Let’s write a simple  for_01.sh script. In this script, we will print the var variable 10 times:

for_01.sh

#!/bin/bash 
for var in {1..10} 
do 
  echo $var 
done 

Let’s test the program:

$ chmod +x for_01.sh
$ ./for_01.sh

The following will be the output after executing the preceding commands:

Output:

12345678910

The following  for_02.sh script uses the C programming style syntax:

for_02.sh

#!/bin/bash 
max=10 
for  ((i=1; i<=max;  i++)) 
do 
echo -n "$i     "    # one case with echo without -n option 
done 

Let’s test the program:

$ chmod +x for_02.sh
$ ./for_02.sh

The following will be the output after executing the preceding commands:

Output:

$ ./for_02.sh      # OUTPUT with -n option1     2     3     4     5     6     7     8     9     10
$ ./for_02.sh      # OUTPUT without -n option12345678910

In the next  for_03.sh script, we will be processing a list of numbers, which are listed next to the in keyword:

for_03.sh

#!/bin/bash 
for var in 11 12 13 14 15 16 17 18 19 20 
do 
  echo $var 
done 

Let’s test the program:

$ chmod +x for_03.sh
$ ./for_03.sh

The following will be the output after executing the preceding commands:

Output:

$ ./for_03.sh
11121314151617181920

In the following  for_04.sh script, we create user11 to user20, along with their home directory:

for_04.sh

#!/bin/bash 
for var in user{11..20} 
do 
  useradd -m $var 
  passwd -d $var 
done 

Let’s test the program:

$ chmod +x for_04.sh
$ sudo ./for_04.sh

After executing the preceding command,user11touser20will be created with their home folders in the/home/folder. You need to be a rootuseror administrator torunthis script.

In thefor_05.shscript, we will be passing command-line parameters. All the command-line parameters will be available as the$*inside script:

for_05.sh

#!/bin/sh 
for var in $* 
do 
  echo "command line contains: $var" 
done 

Let’s test the program:

$ chmod +x for_05.sh
$ ./for_05.sh 1 2 3 4 5 6

The following will be the output after executing the preceding commands:

Output:

command line contains: 1command line contains: 2command line contains: 3command line contains: 4command line contains: 5command line contains: 6

In the next for_06.sh script, we are passing a list of words, such as the names of fruits. Inside the script, we are printing the information of the variable:

for_06.sh

#!/bin/bash 
# create fruits.txt => Apple Mango Grapes Pears Banana Orange Pineapple 
for var in `cat fruits.txt` 
do 
    echo "var contains: $var" 
done 

Let’s test the program:

$ chmod +x for_06.sh
$ ./for_06.sh

The following will be the output after executing the preceding commands:

Output:

var contains: Applevar contains: Mangovar contains: Grapesvar contains: Pearsvar contains: Bananavar contains: Orangevar contains: Pineapple

Using thefor_07.shscript, we generate a list of files with thelsshell command. This will be the list of filenames. In theforloop, the following list of files will be printed:

for_07.sh

#!/bin/bash 
echo -n "Commands in bin directory are : $var" 
for var in $(ls /bin/*) 
do 
        echo -n -e "$var t" 
done 

Let’s test the program:

$ chmod +x for_07.sh
$ ./for_07

The following will be the output after executing the preceding commands:

Output:

This will print the content of /bin/ directory.

For taking a backup of the files, we can write the for_08.sh script as follows:

for_08.sh

#!/bin/bash 
for filename in *.c 
do 
  echo "Copying $filename to $filename.bak" 
  cp $filename $filename.bak  
done 

Let’s test the program:

$ chmod +x for_08.sh
$ touch 1.c 2.c
$ ./for_08.sh

The following will be the output after executing the preceding commands:

Output:

"Copying 1.c to 1.c.bak""Copying 2.c to 2.c.bak"

Back to Featured Articles on Logo Paperblog