How while loop execute in the shell script for the FALSE condition?

I tried it with some example but it is not clear to me.


while [ $i -lt 10 ]
    echo "$i"
    i=`echo "$i + 1" | bc`

output ===> 0,1,2,3,4,5,6,7,8,9

now when i executed while “while [ true ] ” it gives the output as infinite loop //I agree//
but when i executed while “while [ false ] ” again it gives the output as infinite loop. /I dont agree/

Can you please explain the second while loop? and how it is working actually?

while [ false ]    #give the infinite loop
while [ true ]     #give the infinite loop
while [ 0 ]        #give the infinite loop
while [ 1 ]        #give the infinite loop
while [ `ls` ]
while [ `echo 1` ] #give the infinite loop
while((0))         #loop not execute
while((1))         #give the infinite loop


In while [ false ] the false is neither a command nor a boolean value. The while expects a command but [ ... ] with no operators just checks for any non-empty string. Thus [ false ] is true. It is in this sense the same as [ faaaalseeee ].

What you meant is:

while true; do ...

while false; do ...

Leave a Reply

Your email address will not be published. Required fields are marked *