SoFunction
Updated on 2025-03-10

Summary of for loops in Shell

There are many usages of for loops in shells, and I have always wanted to summarize them. Today I saw the previous summary of the usage of for loops on the Internet. I felt that it was very comprehensive, so I turned to study and research, hehe...

Copy the codeThe code is as follows:
for((i=1;i<=10;i++));do echo $(expr $i \* 4);done

The commonly used shell is for i in $(seq 10)
Copy the codeThe code is as follows:

for i in `ls`
for i in ${arr[@]}
for i in $* ; do
for File in /proc/sys/net/ipv4/confaccept_redirects:'
for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo $File
done
echo "Specify loop content directly"
for i in f1 f2 f3 ;do
echo $i
done
echo
echo "C syntax for loop:"
for (( i=0; i<10; i++)); do
echo $i
done

---------------------------------------------------------------------------------------------------------
Usage of for loop in shell

Shell syntax is so troublesome. After a while, I found several different methods to output 1-100 numbers that can be divisible by 3 between 1 and 100.
1. Use (())

Copy the codeThe code is as follows:

#!/bin/bash
clear
for((i=1;i<100;i++))
for
do
if((i%3==0))
then
echo $i
continue
fi
done

2. Use `seq 100`
Copy the codeThe code is as follows:

#!/bin/bash
clear
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done

3. Use while
Copy the codeThe code is as follows:

#!/bin/bash
clear
i=1
while(($i<100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done

--------------------------------------------------------------------------------------------------------
When the shell uses the for loop to increment the number, I found a problem. I will list several methods for the for loop under the shell:
1.
Copy the codeThe code is as follows:

for i in `seq 1 1000000`;do
echo $i
done

Use seq 1 10000000 to increment. I didn't encounter any problems when using this method before, because the previous i didn't use millions (1000000). Because the project requires me to have this number much larger than millions. I found that when using seq value to 1000000, it was converted to 1e+06. It is impossible to perform other operations as a number, or use $i effectively and correctly, so I found other methods to solve it.
2.
Copy the codeThe code is as follows:

for((i=1;i<10000000;i++));do
echo $i
done
3.
i=1
while(($i<10000000));do
echo $i
i=`expr $i + 1`
done

Because this method calls expr, the running speed will be much slower than the first one, but it can be slightly improved. Change i=`expr $i + 1` to i=$(($i+1)) to slightly increase the speed, but it depends on whether the corresponding shell environment supports it.
4.
Copy the codeThe code is as follows:

for i in {1..10000000;do
echo $i
done

In fact, which method to choose should be specifically supported by the corresponding shell environment to achieve the expected results, and then consider the speed issue.
Example:
Copy the codeThe code is as follows:

# !/bin/sh
i=1
function test_while(){
i=1
while [ $i ]
do
echo $i
i=`expr $i + 1`
if [ $i -ge 10 ]; then
break
fi
done
}
function test_for(){
i=1
for ((i=1; i<=100; i++)); do
echo $i
if [ $i -ge 10 ]; then
break
fi
done
}
function test_continue(){
i=1
for i in $(seq 100); do
if (( i==0 )); then
echo $i
continue
fi
done
}
echo "test_while..."
test_while
echo "test_for..."
test_for
echo "test_continue..."
test_continue

Running results:
Copy the codeThe code is as follows:

test_while...
1
2
3
4
5
6
7
8
9
test_for...
1
2
3
4
5
6
7
8
9
10
test_continue...
10
20
30
40
50
60
70
80
90
100