Loops in bash

Loops in bash

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

While Loop
For Loop
Until loop


#bash #linux

要查看或添加评论,请登录

Soheil Pasbakhsh的更多文章

  • Quick explaination of how git works

    Quick explaination of how git works

    Almost every developers uses git for their day to day task, but not every one knows about how it actually works. If you…

    2 条评论
  • Quick fix for EADDRINUSE error in linux

    Quick fix for EADDRINUSE error in linux

    You might get EADDRINUSE error in some situations which means a port in the system that you want to use is being used…

社区洞察

其他会员也浏览了