What does exit do in an if block in a shell script?

17,610

Solution 1

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.

Solution 2

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.

Share:
17,610
jamesT
Author by

jamesT

Updated on September 18, 2022

Comments

  • jamesT
    jamesT over 1 year

    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
        fi
        echo "You have access!"
        exit 1
    fi
    
    • Admin
      Admin over 11 years
      I assume that those semantics are specific to the used shell. Which shell is executing the script?
    • Admin
      Admin over 11 years
      It would probably have been faster to just test it.