I have a question regarding unix shell script.

Say if you do exit 1 in inner if: will it exit or will it still execute the outer if? The following is a dummy example.

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
    if [ "$PASSWORD" -gt 10]; then
        echo "password is too large!"
        exit 1
    echo "You have access!"
    exit 1
  • I assume that those semantics are specific to the used shell. Which shell is executing the script? – kronn Oct 23 '12 at 9:22
  • that was just example, but developing for kshell. – jamesT Oct 23 '12 at 9:31
  • It would probably have been faster to just test it. – Jim Paris Oct 23 '12 at 17:15

exit is usually a shell built-in, so in theory it does depend on which shell you're using. However, I'm not aware of any shell where it operates other than exiting the current process. From the bash man page,

   exit [n]
          Cause the shell to exit with a status of n.  If  n  is  omitted,
          the exit status is that of the last command executed.  A trap on
          EXIT is executed before the shell terminates.

So it doesn't simply end the current if clause, it exits the whole shell (or process, in essence, since the script is being run within a shell process).

From man sh,

 exit [exitstatus]
        Terminate the shell process.  If exitstatus is given it is used as
        the exit status of the shell; otherwise the exit status of the
        preceding command is used.

And lastly, from man ksh,

   † exit [ n ]
          Causes  the  shell  to exit with the exit status specified by n.
          The value will be the least significant 8 bits of the  specified
          status.   If  n  is omitted, then the exit status is that of the
          last command executed.  An end-of-file will also cause the shell
          to  exit  except for a shell which has the ignoreeof option (see
          set below) turned on.

exit terminates the calling process. In most circumstances, this exits the whole script, even if you call it from inside a loop, a function or an included script. The only shell constructs that “catch” exit are the ones that introduce a subshell (i.e. a forked child shell process):

  • the basic subshell construct (…) that executes the command inside the parentheses in a subshell;
  • the command substitution construct $(…) (and its deprecated equivalent, the backquotes `…`), which executes a command and returns its output as a string;
  • background jobs forked with &;
  • the left-hand side of a pipe |, and the right-hand side as well on most shells (ATT ksh and zsh being the exceptions);
  • some shells have other subprocess constructs, for example process substitution <(…), >(…), etc., on ksh, bash and zsh.

You can break out of a while or for loop with the break keyword, and you can break out of a function with the return keyword.


exit will end the script completely.

break will go one level higher in loops - but not in if-statements (according to my bash-manpage).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.