Shell environment and subshells
The shell execution environment is the set of state the shell maintains to control its behavior. It consists of:
- File descriptors
- Working directory
- File creation mask
- Resource limits
- Variables
- Positional parameters
- Values of these special parameters:
?
: exit status of the last command$
: process ID of the shell!
: process ID of the last asynchronous command0
: name of the shell or script
- Functions
- Aliases
- Shell options
- Traps
- Jobs
Subshells
A subshell is a separate environment created as a copy of the current shell environment. Changes in a subshell do not affect the parent shell. A subshell starts with the same state as the parent, except that traps with custom commands are reset to default behavior.
Create a subshell using parentheses. Subshells are also created implicitly when running an external utility, a command substitution, an asynchronous command, or a multi-command pipeline.
Subshells of an interactive shell are not themselves interactive, even if the interactive
option is set.
Yash-rs currently implements subshells using the fork
system call, which creates a new process. This may change in the future for greater efficiency.
External utilities run by the shell inherit the following from the shell environment:
- File descriptors
- Working directory
- File creation mask
- Resource limits
- Environment variables
- Traps, except those with a custom command