Contents
How is a subshell created in a shell?
A subshell starts out as an almost identical copy of the original shell process. Under the hood, the shell calls the fork system call 1, which creates a new process whose code and memory are copies 2. When the subshell is created, there are very few differences between it and its parent.
What’s the difference between parent and subshell in Bash?
When the subshell is created, there are very few differences between it and its parent. In particular, they have the same variables. Even the $$ special variable keeps the same value in subshells: it’s the original shell’s process ID. Similarly $PPID is the PID of the parent of the original shell.
How are parentheses used in a subshell environment?
Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, […] Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question.
How to change the PID of a subshell in Bash?
Even the $$ special variable keeps the same value in subshells: it’s the original shell’s process ID. Similarly $PPID is the PID of the parent of the original shell. A few shells change a few variables in the subshell. Bash sets BASHPID to the PID of the shell process, which changes in subshells.
Can a subshell start out with the same current directory?
The subshell also starts out with the same current directory, the same signal mask, etc. One of the few exceptions is that subshells do not inherit custom traps: ignored signals ( trap ” SIGNAL) remain ignored in the subshell, but other traps ( trap CODE SIGNAL) are reset to the default action 4.
How is a parenthesized command executed in a subshell?
A parenthesized command is executed in a sub-shell without removing non-exported variables. list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).
What does a subshell do in Windows 10?
A subshell is a separate instance of the command processor — the shell that gives you the prompt at the console or in an xterm window. Just as your commands are interpreted at the command-line prompt, similarly does a script batch-process a list of commands.