0

Is there a way to end a while loop by using an if? Here's just some pseudocode of the basics of what I want to do:

re = 1
while [re = 1]
do
    echo "hello, enter name"
    read name
    echo name
    echo "continue? 1 for yes, 0 for no"
    read re
    if [re == 1]
    then
        pass
    elif [re == 0]
    then
        end while
    else
        pass
0

2 Answers 2

1

It looks like you need to use continue in place of pass if you want to skip the rest of the loop and start again from the beginning, and you need to use break instead of end while if you want to exit the loop altogether.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, bash supports the break command to do that:

 i=0; 
 while [ $i -lt 10 ]; do 
    if [ $i -eq 3 ]; then 
       break 
    fi
    echo $i
    let i++
 done

Will output:

 0
 1
 2 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.