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
C
language 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
Leave a message
Disclaimer
- Welcome to visit the knowledge base of SRE and DevOps!
- License under CC BY-NC 4.0
- Made with Material for MkDocs and improve writing by generative AI tools
- Copyright issue feedback me#imzye.com, replace # with @