You can exit a script at any place using the keyword exit . You can also specify an exit code in order to indicate to other programs that or how your script failed, e.g. exit 1 or exit 2 etc.
How do you exit a script?
If a script ends with exit without specifying a parameter, the script exit code is that of the last command executed in the script. Using just exit is the same as exit $? or omitting the exit . If you run the script as root, the exit code will be zero. Otherwise, the script will exit with status 1 .
How do I break out of a bash script?
You can use the break command to exit from any loop, like the while and the until loops. The loop runs until it reaches 14 then the command exits the loop. The command exits the while loop, and that happens when the execution reaches the if statement.