Repeating with loops
Bash loops are iteration statements, a repetition of a process. Imagine that you have the output of many lines of data from log files or vulnerability scans. Reviewing each line manually would be akin to climbing a mountain with your hands tied; possible, but unnecessarily challenging. Bash loops, with their simple syntax and versatile application, turn this mountain into a molehill. In this section, we’ll dive into the essence of Bash loops, exploring their types, how they work, and why they’re an indispensable part of scripting in Linux environments.
The for loop
The for loop is your go-to when you know how many times you want to repeat an action. It’s like saying, “For each item in this list, do this task.” Bash for
loops iterate over a list of items or a range of values, executing commands for each item. Here’s the basic for
loop syntax:
for variable in list do command1 command2 ...