Different loop style in Bash
Introduction
Bash is one of the most commonly used shell programming languages in the Linux environment. One of the essential features of bash is its ability to execute repetitive tasks using loops. Loops are used to iterate over a list of items and perform a set of actions on each item. In this blog post, we will discuss the different loop styles in Bash and how they can be used.
for loop
#!/usr/bin/env bash
for i in `seq 1 10`
do
#code here
echo $i
done
another for loop
Clanguage style
#!/usr/bin/env bash
for ((i=1; i<=10; i++))
do
printf "%s\n" "$i"
done
while loop
#!/usr/bin/env bash
i=1
while [[ "$i" -lt "10" ]]
do
#code here
echo $i
#i=$[ $i + 1 ]
((i++))
done
until loop
#!/usr/bin/env bash
until [[ "$i" -ge "10" ]]
do
echo $i
#statements to be executed as long as the condition is false
((i++))
done
Some of the content is generated by AI, please be cautious in identifying it.