parenthesis
Shell parentheses for subshells, arrays, and arithmetic
TLDR
SYNOPSIS
( commands )((arithmetic))
DESCRIPTION
Parentheses ( ) in shell have several distinct uses depending on context:Subshell execution: Commands in (...) run in a child shell. Environment changes (cd, variable assignments) don't affect the parent shell.Array literals: In bash/zsh, array=(a b c) creates an array.Function definition: name() { ... } defines a function (parentheses are part of syntax, not grouping).Command substitution: $(...) captures command output.Arithmetic: ((...)) performs arithmetic evaluation, $((...)) expands to the result.
SUBSHELL BEHAVIOR
(cd /tmp; pwd) # prints /tmp
pwd # still original directory
# Variables don't leak
(x=5)
echo $x # empty or original value
ARITHMETIC (( ))
(( x++ )) # Increment
(( x > 5 )) && echo "big" # Condition
result=$(( a * b )) # Capture result
CAVEATS
Subshells have overhead from process creation. For simple grouping without isolation, use braces { ...; } instead.{ } grouping requires a space after { and semicolon before }; parentheses don't.Subshell exit status is visible to parent, but variable changes are not.Nested parentheses may need careful quoting to avoid syntax issues.
