Bash loops are very useful. Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks. There are 3 basic loop structures in Bash scripting
- While Loops
While an expression is true, keep executing these lines of code.
while [ $counter -le 10 ]
2) For Loop:
The for loop is a little bit different to the previous two loops. What it does is say for each of the items in a given list, perform the given set of commands. It has the following syntax.
for var in <list>
The for loop will take each item in the list (in order, one after the other), assign that item as the value of the variable var, execute the commands between do and done then go back to the top, grab the next item in the list and repeat over.
The list is defined as a series of strings, separated by spaces.
for x in audi bmw tata;do