Thursday, March 7, 2013

brackets, parentheses, curly braces in BASH

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*}
  • Make substitutions similar to sed
    $ var="abcde"; echo ${var/de/12}
  • Use a default value
    $ default="hello"; unset var; echo ${var:-$default}
  • and several more
Double parentheses are used for arithmetic operations:

((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:

Curly brace are required for (most/all?) array references on the right hand side.
 parentheses are also used for subshells. And that they are used to create arrays.
array=(1 2 3)
echo ${array[1]}

  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 ; fi
  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 ; fi
  3. Parentheses (()) are used to create a subshell. For example:
    $ pwd
    $ (cd /tmp; pwd)
    $ pwd
    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:
    $ VARIABLE=abcdef
    $ echo Variable: $VARIABLE
    Variable: abcdef
    $ echo Variable: $VARIABLE123456
    $ echo Variable: ${VARIABLE}123456
    Variable: abcdef123456
4b. 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.


1 comment:

  1. Overall, punctuation can be a bit of a keep for most authors to cope with. Commas, colons, semi-colons... they all seem to have many features and it's never quite obvious how and when to use them properly. One way of punctuation that can often be complicated is the parentheses. First of all, the name often befuddles individuals. One parenthesis is just that a "parenthesis" with an "i." Two, are parentheses. Still, the two conditions are often puzzled, mainly because you never use only one parenthesis. And therein can be found one of the most typical mistakes when using parentheses-forgetting to "close" at the end of your parenthetical. For example, if you want add some extra information-a part observe, if you will-into your composing, you can do so with parentheses Parenthesis, essentially