How to break out of a loop in Bash? How to break out of a loop in Bash? bash bash

How to break out of a loop in Bash?


It's not that different in bash.

workdone=0while : ; do  ...  if [ "$workdone" -ne 0 ]; then      break  fidone

: is the no-op command; its exit status is always 0, so the loop runs until workdone is given a non-zero value.


There are many ways you could set and test the value of workdone in order to exit the loop; the one I show above should work in any POSIX-compatible shell.


while true ; do    ...    if [ something ]; then        break    fidone