LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

parenthesis

Shell parentheses for subshells, arrays, and arithmetic

TLDR

Run commands in a subshell
$ (cd [/tmp] && [command])
copy
Create an array
$ array=([one] [two] [three])
copy
Command grouping without subshell
$ { [command1]; [command2]; }
copy
Function definition
$ function_name() { [commands]; }
copy
Arithmetic evaluation
$ (( count++ ))
copy

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

$ # Changes don't affect parent
(cd /tmp; pwd)  # prints /tmp
pwd             # still original directory

# Variables don't leak
(x=5)
echo $x         # empty or original value
copy

ARITHMETIC (( ))

$ (( x = 5 + 3 ))    # Assignment
(( x++ ))          # Increment
(( x > 5 )) && echo "big"  # Condition
result=$(( a * b ))         # Capture result
copy

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.

SEE ALSO

bash(1), sh(1), test(1)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid