How to use double or single brackets, parentheses, curly braces How to use double or single brackets, parentheses, curly braces bash bash

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


In Bash, test and [ are shell builtins.

The double bracket, which is a shell keyword, enables additional functionality. For example, you can use && and || instead of -a and -o and there's a regular expression matching operator =~.

Also, in a simple test, double square brackets seem to evaluate quite a lot quicker than single ones.

$ time for ((i=0; i<10000000; i++)); do [[ "$i" = 1000 ]]; donereal    0m24.548suser    0m24.337ssys 0m0.036s$ time for ((i=0; i<10000000; i++)); do [ "$i" = 1000 ]; donereal    0m33.478suser    0m33.478ssys 0m0.000s

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:

$ echo f{oo,ee,a}dfood feed fad$ mv error.log{,.OLD}(error.log is renamed to error.log.OLD because the brace expressionexpands to "mv error.log error.log.OLD")$ for num in {000..2}; do echo "$num"; done000001002$ echo {00..8..2}00 02 04 06 08$ echo {D..T..4}D H L P T

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:

((a++))((meaning = 42))for ((i=0; i<10; i++))echo $((a + b + (14 * c)))

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:

array[4]="hello"element=${array[index]}

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.

array=(1 2 3)echo ${array[1]}2


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

    $ VARIABLE=abcdef$ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fiyes
  2. The double bracket ([[) does the same thing (basically) as a single bracket, but is a bash builtin.

    $ VARIABLE=abcdef$ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fino
  3. Parentheses (()) are used to create a subshell. For example:

    $ pwd/home/user $ (cd /tmp; pwd)/tmp$ pwd/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:

    $ VARIABLE=abcdef$ echo Variable: $VARIABLEVariable: abcdef$ echo Variable: $VARIABLE123456Variable:$ echo Variable: ${VARIABLE}123456Variable: abcdef123456

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

    $ { date; top -b -n1 | head ; } >logfile # 'date' and 'top' output are concatenated, # could be useful sometimes to hunt for a top loader )$ { date; make 2>&1; date; } | tee logfile# now we can calculate the duration of a build from the logfile

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.


Brackets

if [ CONDITION ]    Test construct  if [[ CONDITION ]]  Extended test construct  Array[1]=element1   Array initialization  [a-z]               Range of characters within a Regular Expression$[ expression ]     A non-standard & obsolete version of $(( expression )) [1]

[1] http://wiki.bash-hackers.org/scripting/obsolete

Curly Braces

${variable}                             Parameter substitution  ${!variable}                            Indirect variable reference  { command1; command2; . . . commandN; } Block of code  {string1,string2,string3,...}           Brace expansion  {a..z}                                  Extended brace expansion  {}                                      Text replacement, after find and xargs

Parentheses

( command1; command2 )             Command group executed within a subshell  Array=(element1 element2 element3) Array initialization  result=$(COMMAND)                  Command substitution, new style  >(COMMAND)                         Process substitution  <(COMMAND)                         Process substitution 

Double Parentheses

(( var = 78 ))            Integer arithmetic   var=$(( 20 + 5 ))         Integer arithmetic, with variable assignment   (( var++ ))               C-style variable increment   (( var-- ))               C-style variable decrement   (( var0 = var1<98?9:21 )) C-style ternary operation