Linux while Command: Creating Conditional Loops in Shell Scripts
The shell has structures to test conditions and execute certain program sequences multiple times (loop), until the tested condition is satisfied.
The while command in Linux continuously tests an expression, until an expression has a return variable equal to zero or true.
In this example, the variable $i gets the initial value equal to zero.
While (while) the condition of the test command represented by [ ] returns zero (true), the loop will continue to be executed.
In the loop the variable $i is incremented, until $i reaches the condition of not being less than 4:
$ i=0
$ while [ $i -lt 4 ]; do echo "conta $i" ; i=$[$i+1]; doneconta 0conta 1conta 2conta 3
The while needs to have an exit condition satisfied, otherwise it will stay in infinite loop.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.