Loops in bash
Soheil Pasbakhsh
Tech-Driven Frontend Lead & Backend Developer | Expertise in JavaScript, Typescript, React.js, Node.js, Nest.js
There are mostly two types of loops in Bash/Linux, for loop and while loop
While loop has a condition that as long as it's satisfied the loop will run and is the break point of the loop, meaning when it doesn't satisfy the loop will terminate.
On the other hand, the for loop condition is a list that the loop will go through and do something for each iteration
Note that in the examples below I put a ";" after the condition usually it's not needed since you will write it in multiple lines if you are writing a bash script but if you want to write a loop in one line e.g. in the command line itself you need that, it's optional in multi-line commands, and also it's a separator of commands so you can write two or more commands in one line separated by a semicolon
The For and while loops are the most common loops, but there is also an "until loop" which is the reverse of the while loop, meaning as long as the condition is evaluated to false it will run
You can see a while loop in action here: https://github.com/mssoheil/network-scanner
I hope it was informative