Post

Bash括号使用

How to use double or single brackets, parentheses, curly braces in Bash

  1. A single bracket ([) usually actually calls a program named [; man test or man [ for more info. Example:

    1
    2
    3
    
    $ VARIABLE=abcdef
    $ <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$VARIABLE</span> == abcdef ] ; <span class="hljs-keyword">then</span> <span class="hljs-built_in">echo</span> <span class="hljs-built_in">yes</span> ; <span class="hljs-keyword">else</span> <span class="hljs-built_in">echo</span> no ; <span class="hljs-keyword">fi</span>
    <span class="hljs-built_in">yes</span>
    
  2. The double bracket ([[) does the same thing (basically) as a single bracket, but is a bash builtin.

    1
    2
    3
    
    $ VARIABLE=abcdef
    $ <span class="hljs-keyword">if</span> [[ <span class="hljs-variable">$VARIABLE</span> == 123456 ]] ; <span class="hljs-keyword">then</span> <span class="hljs-built_in">echo</span> <span class="hljs-built_in">yes</span> ; <span class="hljs-keyword">else</span> <span class="hljs-built_in">echo</span> no ; <span class="hljs-keyword">fi</span>
    no
    
  3. Parentheses (()) are used to create a subshell. For example:

    1
    2
    3
    4
    5
    6
    
    $ <span class="hljs-built_in">pwd</span>
    /home/user 
    $ (<span class="hljs-built_in">cd</span> /tmp; <span class="hljs-built_in">pwd</span>)
    /tmp
    $ <span class="hljs-built_in">pwd</span>
    /home/user
    

    As you can see, the subshell allowed you to perform operations without affecting the environment of the current shell.

  4. (a) Braces ({}) are used to unambiguously identify variables. Example:

    1
    2
    3
    4
    5
    6
    7
    
    $ VARIABLE=abcdef
    $ <span class="hljs-built_in">echo</span> Variable: <span class="hljs-variable">$VARIABLE</span>
    Variable: abcdef
    $ <span class="hljs-built_in">echo</span> Variable: <span class="hljs-variable">$VARIABLE123456</span>
    Variable:
    $ <span class="hljs-built_in">echo</span> Variable: <span class="hljs-variable">${VARIABLE}</span>123456
    Variable: abcdef123456
    

    (b) Braces are also used to execute a sequence of commands in the current shell context, e.g.

    1
    2
    3
    4
    5
    6
    
    $ { <span class="hljs-built_in">date</span>; top -b -n1 | <span class="hljs-built_in">head</span> ; } &gt;logfile 
    <span class="hljs-comment"># 'date' and 'top' output are concatenated, </span>
    <span class="hljs-comment"># could be useful sometimes to hunt for a top loader )</span>
        
    $ { <span class="hljs-built_in">date</span>; make 2&gt;&amp;1; <span class="hljs-built_in">date</span>; } | <span class="hljs-built_in">tee</span> logfile
    <span class="hljs-comment"># now we can calculate the duration of a build from the logfile</span>
    

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.

Source: StackOverflow

This post is licensed under CC BY 4.0 by the author.