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*}
    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
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.
 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 ; fi
    yes
  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
    no
  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.
4a. Braces ({}) are used to unambiguously identify variables. Example:
    $ VARIABLE=abcdef
    $ echo Variable: $VARIABLE
    Variable: abcdef
    $ echo Variable: $VARIABLE123456
    Variable:
    $ 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.

From: http://stackoverflow.com/questions/2188199/bash-double-or-single-bracket-parentheses-curly-braces

9 comments:

  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

    ReplyDelete
  2. You brought you kid into the dental specialist and get the horrible news: he needs supports. He has a slight overbite that should be fixed. The idea of having massive, unappealing supports alarms your youngster. Info about Braces & Invisalign

    ReplyDelete
  3. However, did you realize that fluoride can be harmful in high amounts? Fluoride can help forestall pits in kids, yet grown-ups have no requirement for it. Natural Toothpaste

    ReplyDelete
  4. When you choose a natural alternative, you're choosing the healthier option compared to the common chemical-filled toothpaste found in most stores. natural toothcare

    ReplyDelete
  5. At times it seems like you're simply discarding cash each month. AdWords is intense space since you're continually rivaling many different dental specialists and organizations with financial plans that may very well overwhelm yours. Get info

    ReplyDelete
  6. The dentist might cause dread in your heart and you would try not to visit a dentist. Invisalign Near Me

    ReplyDelete
  7. I am so grateful for your blog. Really looking forward to read more. Much obliged.
    Visit this all on four site

    ReplyDelete
  8. Thanks for sharing this is a fantastic article. Really thank you! Awesome.
    best dental implants in Brisbane

    ReplyDelete
  9. He explained the importance of a balanced diet for oral health.
    Dentist

    ReplyDelete