Bash Until Loop
Bash until loop works exactly in reverse way as while loop works. In until loop we can repeat code and statements till condition expression fails. let’s try to understand its working and syntax with some examples for until loop
Syntax Bash Until Loop
until [ condition expression ];do commands statement done |
With this syntax, we can easily say this is same as of while loop, But there is small difference in between while and until. until work only in case provided conditional expression get fail, in reverse while loop work only in case condition expression get success. Let see some examples to see how until work and difference between both while and until.
#!/bin/bash count=1 until [ $count -ge 10 ];do echo $count (( count++ )) done ===================================== OutPut #./bash_until 1 2 3 4 5 6 7 8 9 |
As we can see, it works only till condition expression was false, as count value reach to 10 condition expression became true and it exit .In reverse while loop will work only when true
#!/bin/bash count=1 while [ $count -ge 10 ];do echo $count (( count++ )) done ===================================== OutPut in verbose mode #bash -x ./bash_while_2 + count=1 + ‘[‘ 1 -ge 10 ‘]’ |
Here we executed it in verbose mode, because otherwise it will not show anything. So in while when it execute it compare count value with 10. as it was not greater and equals to 10, it exit nothing comes out of it.
Leave a Reply