What is the difference between `( ... )` and `{ ...; }`?
BeginnerConceptArguments, I/O, here-docs, process substitution and subshells
Answer
Both group commands so you can redirect or chain them as one unit. ( ) runs in a subshell (isolated state, extra fork). { ...; } runs in the current shell (variables and exit affect the script).
{ echo "== disk =="; df -h; echo "== mem =="; free -m; } > report.txt
[[ -f $cfg ]] || { echo "missing $cfg" >&2; exit 1; } # exit the script
[[ -f $cfg ]] || ( echo "missing" >&2; exit 1 ) # exits only the subshell!
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