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?
相关问题
- What does an “empty line with semicolon” mean in C
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- What is the meaning of this syntax?
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
In Bash,
test
and[
are builtins.The double bracket enables additional functionality. For example, you can use
&&
and||
instead of-a
and-o
and there's a regular expression matching operator=~
.The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like:
Truncate the contents of a variable
$ var="abcde"; echo ${var%d*}
abc
Make substitutions similar to
sed
$ var="abcde"; echo ${var/de/12}
abc12
Use a default value
$ default="hello"; unset var; echo ${var:-$default}
hello
and several more
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
I just wanted to add these from TLDP:
Parentheses in function definition
Parentheses
()
are being used in function definition:That is the reason you have to escape parentheses even in command parameters:
A single bracket (
[
) usually actually calls a program named[
;man test
orman [
for more info. Example:The double bracket (
[[
) does the same thing (basically) as a single bracket, but is a bash builtin.Parentheses (
()
) are used to create a subshell. For example:As you can see, the subshell allowed you to perform operations without affecting the environment of the current shell.
4a. Braces (
{}
) are used to unambiguously identify variables. Example: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
( )
, though (see bash reference) ; essentially, a semicolon;
after the last command within braces is a must, and the braces{
,}
must be surrounded by spaces.The difference between test, [ and [[ is explained in great details in the BashFAQ.
And the conclusion: