Bash Scripting: Understanding the Use of Parentheses, Brackets, and Braces

In this tutorial, we will explore the various uses of parentheses, brackets, and braces in BASH scripting. These symbols have distinct functionalities and are crucial for writing effective scripts. Understanding their uses can help you in tasks such as arithmetic operations, test constructs, and parameter expansions.

In this tutorial you will learn:

  • How to use double parentheses for arithmetic operations
  • How to use square brackets for test constructs
  • How to use double square brackets for regular expressions
  • How to use curly braces for variable delimitation and parameter expansion
BASH Scripting: Understanding the Use of Parentheses, Brackets, and Braces
BASH Scripting: Understanding the Use of Parentheses, Brackets, and Braces
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based system
Software BASH shell
Other Basic understanding of shell scripting
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Using Parentheses, Brackets, and Braces in BASH

In BASH scripting, parentheses, brackets, and braces are used for different purposes. Below are some examples to illustrate their uses:

  1. Using Double Parentheses for Arithmetic: Double parentheses are used for arithmetic operations in BASH.
    ((var++))
    ((var = 3))
    for ((i = 0; i < VAL; i++))
    echo $((var + 2))
    

    In this example, ((var++)) increments the variable var, ((var = 3)) assigns the value 3 to var, and the for loop uses double parentheses for initialization, condition, and increment.

  2. Using Square Brackets for Test Constructs: Square brackets are used for test constructs in BASH.
    $ VAR=2
    $ if [ $VAR -eq 2 ]
    > then
    > echo 'yes'
    > fi
    yes
    

    In this example, square brackets are used to test if the variable VAR equals 2.



  3. Using Double Square Brackets for Regular Expressions: Double square brackets offer extended functionality for regular expressions.
    $ VAR='some string'
    $ if [[ $VAR =~ [a-z] ]]; then
    > echo 'is alphabetic'
    > fi
    is alphabetic
    

    In this example, double square brackets are used to check if the variable VAR contains alphabetic characters using the regular expression [a-z].

  4. Using Curly Braces for Variable Delimitation: Curly braces are used to delimit a variable.
    $ foo='stage'
    $ echo $fooone
               ... returns empty line
    $ echo ${foo}one
    stageone
    

    In this example, curly braces are used to correctly concatenate the value of the variable foo with the string one.

  5. Using Curly Braces for Parameter Expansion: Curly braces are also used for parameter expansion.
    $ var="abcdefg"; echo ${var%d*}
    abc
    

    In this example, the parameter expansion ${var%d*} removes the shortest match of d* from the end of the variable var, resulting in abc.

Conclusion

In this tutorial, we briefly outlined some of the major use cases for parentheses, brackets, and braces in BASH scripting. These symbols are essential tools in your scripting arsenal. Remember, double parentheses for arithmetic, single square brackets for test constructs, double square brackets for regular expressions, and curly braces for variable delimitation and parameter expansion. Good luck with your BASH scripting!



Comments and Discussions
Linux Forum