Shell Script – Why Does (exit 1) Not Exit the Script?

exitshell-scriptsubshell

I have a script, that does not exit when I want it to.

An example script with the same error is:

#!/bin/bash

function bla() {
    return 1
}

bla || ( echo '1' ; exit 1 )

echo '2'

I would assume to see the output:

:~$ ./test.sh
1
:~$

But I actually see:

:~$ ./test.sh
1
2
:~$

Does the () command chaining somehow create a scope? What is exit exiting out of, if not the script?

Best Answer

() runs commands in the subshell, so by exit you are exiting from subshell and returning to the parent shell. Use braces {} if you want to run commands in the current shell.

From bash manual:

(list) list is executed in a subshell environment. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.

{ list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter.

It's worth mentioning that the shell syntax is quite consistent and the subshell participates also in the other () constructs like command substitution (also with the old-style `..` syntax) or process substitution, so the following won't exit from the current shell either:

echo $(exit)
cat <(exit)

While it may be obvious that subshells are involved when commands are placed explicitly inside (), the less visible fact is that they are also spawned in these other structures:

  • command started in the background

    exit &
    

    doesn't exit the current shell because (after man bash)

    If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

  • the pipeline

    exit | echo foo
    

    still exits only from the subshell.

    However different shells behave differently in this regard. For example bash puts all components of the pipeline into separate subshells (unless you use the lastpipe option in invocations where job control is not enabled), but AT&T ksh and zsh run the last part inside the current shell (both behaviours are allowed by POSIX). Thus

    exit | exit | exit
    

    does basically nothing in bash, but exits from the zsh because of the last exit.

  • coproc exit also runs exit in a subshell.

Related Question