What is a subshell, and which constructs create one?
IntermediateConceptArguments, I/O, here-docs, process substitution and subshells
Answer
A subshell is a forked copy of the shell; changes to variables, cd, set options and traps inside it do not affect the parent. Created by:
( commands )- command substitution
$(...) - each part of a pipeline (except the last with
lastpipe) - background jobs
cmd & - process substitution
<(...)
( cd /tmp && tar -czf /backup/tmp.tgz . ) # parent's directory unchanged
echo "$BASH_SUBSHELL" # 0 in parent, 1+ inside subshells
{ commands; } groups without a subshell (note the spaces and the final ;).
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate