Bash 101 Part 4: Brackets / braces / Parenthesis, Arithmetic and Loops

Bash can do arithmetic and loops. While this comes as no surprise, this article intends to show some examples of both and provide a few references where you can learn more.

Also covered: What the different bracket types mean.



Brackets, Parenthesis and braces

Bash uses Brackets and parenthesis to indicate what operations should be performed. Here are a few forms you may find along with a brief description:

  • [ ]
    • Single brackets are used for comparison operations. In the past [ was a command like the unix test command.
  • [[ ]]
    • Double brackets are also used for comparisons, but they expose a richer syntax which enables more testing and comparison operations
  • ( )
    • Single parenthesis call a sub-shell
  • (( ))
    • Double parenthesis are used for arithmetic (demonstrated below)
  • { }
    • Single Braces can be used to:
      • Unambiguously identify variables
      • Define a sequence of commands for the current shell context

For more information see the Stack Overflow article or the Bash documentation


Arithmetic operations

Bash will allow you to perform mathematical operations within a script. For a complete list of supported operations, see the documentation(Shell-Arithmetic)

I am to demonstrate how to use Arithmetic expansion to perform:

  • Addition / Subtration
  • Multiplication / Division

Addition and Subraction

# This script will demonstrate addition and subraction
echo "Sample script to demonstrate addition and subraction"
echo "  "

# Define the variables

# Perform operations & display the result
echo "Addition of $var1 and $var2 = $addition_result"
echo "  "

echo "Subtraction of $var1 from $var2 = $subtraction_result"


Here are the script results:



The process works the same for Multiplication and Division:

# This script will demonstrate Multiplication and Division
echo "Sample script to demonstrate Multiplication and Division"
echo "  "

# Define the variables

# Perform operations & display the result
echo "Multiplication of $var1 and $var2 = $m_result"
echo "  "

echo "Division of $var1 by $var2 = $d_result"


Here is the command output:



Here are the key points to take away (Arithmetic):

  1. Integer variables can be assigned just like strings VARNAME=89
  2. Enclose integer variables in $(( )) to perform operations on them (add/sub/mul/div)
  3. You can capture the result of the numeric operation in a variable for later use/display


Important Note: For floating point operations, see the references listed in the Notes section above. It looks like Bash primarily deals with integers and you need to use external commands or some code to get floating point math to work.



Loops and looping

Bash supports For / For each, while and until loops. I'll be showing examples of for, foreach and while. If you'd like more comprehensive examples or information about these loop styles see the Notes section for additional references.

Example script listing 3 loop types: for, foreach and while:

# This script shows loop examples

# Typical C like For loop
echo "This is what I would consider to be a typical for loop"
for (( i=0; i<5; i++))
    echo "This is the $i'th loop!"

# for-each loop
echo "  "
echo "This is like a foreach loop in C#.Net"
for filename in ~/*
    echo "Filename is $filename"

# While loop
echo "  "
echo "Here's the while loop"
while [ $count -lt 7 ]
    echo "The count is: $count"


Here is the command output of all 3 loops:



Here are the key points to take away (Loops):

  1. The statements between the do and done statements will be repeated during each iteration
  2. For c-type for loop behavior, use the (( var=0; var < 9; var++)) syntax
  3. A for-each loop in bash will repeat once for each statement returned by an operation (ls, read, etc...). In the example above, the foreach loop lists all files in my home directory
  4. A while loop will continue if the criteria contained in the [ $expression ] construct returns true


For more information on the topics covered today, see my reference material listed in the notes section at the top of the article.