How to use Linux bash for loop

Time:2021-5-11

The for loop isLinux shellThe most commonly used structure in.

For loop has three structures: one is list for loop; The second structure is for loop without list; The third structure is a C-like for loop.

This blog post focuses on the list for loop. The format of the list for loop is fixed. There are many scenarios in the composition of the list, such as number list, string list, command list, script parameter transfer list, and so on.
More aboutHow to use bash for loop in Linux

The list for loop statement is used to execute a group of commands for a known number of times. The basic format of the statement is as follows
`

for variable in (list)
do 
    command
    command
   
done

`

Among them, the command between do and done becomes the loop body, and the number of execution times is the same as the number of constants or strings in the list. When the for loop is executed, the first constant or string of the list after in is assigned to the loop variable, and then the loop body is executed; Next, assign the second constant or string in the list to the loop variable and execute the loop body again. This process will continue until there are no other constants or strings in the list, and then execute the command sequence after the done command.

EX1, the case of constant list in list for loop

`#!/bin/bash

#Use the list for loop to display 5 welcome actions
for variable  in 1 2 3 4 5 
do
    echo "Hello, welcome $variable  times "
done`

The loop of this example is often used for counting, and the range is limited to 1-5. The following is the result of the script execution. As there are five parameters listed in the list after in, you can see that the script executes five welcome operations.
[[email protected] shellscript]$ sh for_ex1.sh
Hello, welcome 1 times
Hello, welcome 2 times
Hello, welcome 3 times
Hello, welcome 4 times
Hello, welcome 5 times
[[email protected] shellscript]$

In the Linux shell, the count method of skimming is used in the support list for loop. We will slightly improve the script
Ex2, the list is abbreviated

#!/bin/bash

#Use the list for loop to display 5 welcome actions
for variable  in {1..5} 
do
    echo "Hello, welcome $variable  times "
done

After execution, the result is the same as script 1
[[email protected] shellscript]$ sh for_ex2.sh
Hello, welcome 1 times
Hello, welcome 2 times
Hello, welcome 3 times
Hello, welcome 4 times
Hello, welcome 5 times
[[email protected] shellscript]$

For the above example, we will omit 1 ~ 5 to make it output the same result as example 1

EX3, the list is abbreviated

#!/bin/bash

#Use the list for loop to display 5 welcome actions
for variable  in $(seq 1 5) 
do
    echo "Hello, welcome $variable  times "
done

The SEQ command is the preset external command of Linux, which is generally used to simplify the writing of a pile of numbers. You can refer to the SEQ command commonly used in Linux.

After execution, the result is the same as above, so it will not be pasted repeatedly.

EX4, according to the number of steps jump way to achieve the list

#!/bin/bash

#Use the list for loop to display 5 welcome actions
for variable  in {1..5..2} 
do
    echo "Hello, welcome $variable  times "
done

in {1..5..2}   Realize the number within 1 ~ 5, jump according to the number of steps 2
Run, look at the results

[[email protected] shellscript]$ sh for_ex4.sh
Hello, welcome 1 times
Hello, welcome 3 times
Hello, welcome 5 times
[[email protected] shellscript]$

Ex5 and jump mode were expressed by SEQ

[[email protected] shellscript]$ cat for_ex5.sh

#!/bin/bash

#Use the list for loop to display 5 welcome actions
for variable  in $(seq 1 2 5) 
do
    echo "Hello, welcome $variable  times "
done

[[email protected] shellscript]$ sh for_ex5.sh
Hello, welcome 1 times
Hello, welcome 3 times
Hello, welcome 5 times
[[email protected] shellscript]$

EX6. Use string to represent the list

[[email protected] shellscript]$ cat for_ex6.sh

#!/bin/bash

#Use the list for loop to display the corresponding English from Monday to Sunday
for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday 
do
    echo "$day"
done

[[email protected] shellscript]$ sh for_ex6.sh
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
[[email protected] shellscript]$

EX7. Use the command to represent the list
[[email protected] shellscript]$ cat for_ex7.sh

#!/bin/bash

#Printing arrays with commands
for variable  in `ls /` 
do
    echo "Every directory is $variable "
done

[[email protected] shellscript]$ sh for_ex7.sh
Every directory is bin
Every directory is boot
Every directory is dev
Every directory is etc
Every directory is home
Every directory is lib
Every directory is lost+found
Every directory is media
Every directory is mnt
Every directory is opt
Every directory is proc
Every directory is root
Every directory is sbin
Every directory is selinux
Every directory is srv
Every directory is sys
Every directory is tmp
Every directory is usr
Every directory is var
[[email protected] shellscript]$

The command format here can use $(command) orcommandThe effect is the same, so we won’t show it here.

Ex8, through the script to achieve the list of parameters

[[email protected] shellscript]$ cat for_ex8.sh

#!/bin/bash

echo "number of arguments is $#"

echo "What you input is :"

#Printing arrays with commands
for argument  in "$*" 
do
    echo "$argument "
done

[[email protected] shellscript]$ sh for_ex8.sh 1 hello shell
number of arguments is 3
What you input is :
1 hello shell
[[email protected] shellscript]$

It can be seen that the parameter list can be a number or a string, but the input is separated by a space. If there is a space, the script will think that there is another parameter.
More:
What is Linux

30 linux commands for novices

Common commands and usage of Linux