I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
|
In Bash, The double bracket enables additional functionality. For example, you can use The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like:
Also, brace expansions create lists of strings which are typically iterated over in loops:
Note that the leading zero and increment features weren't available before Bash 4. Thanks to gboffi for reminding me about brace expansions. Double parentheses are used for arithmetic operations:
and they enable you to omit the dollar signs on integer and array variables and include spaces around operators for readability. Single brackets are also used for array indices:
Curly brace are required for (most/all?) array references on the right hand side. ephemient's comment reminded me that parentheses are also used for subshells. And that they are used to create arrays.
|
|||||||||||||||||||||
|
|
Brackets
[1] http://wiki.bash-hackers.org/scripting/obsolete Curly Braces
Parentheses
Double Parentheses
|
|||||||||||||||||||||
|
Parentheses in function definition
Parentheses
That is the reason you have to escape parentheses even in command parameters:
|
|||
4a. Braces (
4b. Braces are also used to execute a sequence of commands in the current shell context, e.g.
There is a subtle syntactic difference with |
|||||||||||||||||||||
|
|
The difference between test, [ and [[ is explained in great details in the BashFAQ.
And the conclusion:
|
|||
|
|
|
I just wanted to add these from TLDP:
|
|||||||||
|