Skip to main content
added 242 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

() 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 twothese 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 runsrun 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.

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

  • coproc exit also runs exit in a subshell.

() 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 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 two 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, but zsh runs the last part inside the current shell. Thus

    exit | exit | exit
    

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

() 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.
added 623 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

() 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 assignments and builtin commands that affect the shell's environment do do not remain in effect after the command completes. The return status 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 or process substitution, so the following won't exit from the current shell either:

echo $(exit)
cat <(exit)

While it may be obvious that subshell issubshells are involved when commands are placed explicitly inside (), the less visible fact is that subshellsthey are also spawned in the pipeline. So the followingtwo other structures:

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, but zsh runs the last part inside the current shell. Thus

exit | exit | exit

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

  • 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, but zsh runs the last part inside the current shell. Thus

    exit | exit | exit
    

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

() runs commands in the subshell, so by exit you are exiting from subshell and returning to 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.


While it may be obvious that subshell is involved when commands are placed explicitly inside (), the less visible fact is that subshells are also spawned in the pipeline. So the following:

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, but zsh runs the last part inside the current shell. Thus

exit | exit | exit

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

() 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 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 two 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, but zsh runs the last part inside the current shell. Thus

    exit | exit | exit
    

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

added 576 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

() runs commands in the subshell, so by exit you are exiting from subshell and returning to 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.


While it may be obvious that subshell is involved when commands are placed explicitly inside (), the less visible fact is that subshells are also spawned in the pipeline. So the following:

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, but zsh runs the last part inside the current shell. Thus

exit | exit | exit

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

() runs commands in the subshell, so by exit you are exiting from subshell and returning to 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.

() runs commands in the subshell, so by exit you are exiting from subshell and returning to 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.


While it may be obvious that subshell is involved when commands are placed explicitly inside (), the less visible fact is that subshells are also spawned in the pipeline. So the following:

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, but zsh runs the last part inside the current shell. Thus

exit | exit | exit

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

Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141
Loading