Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


Math with Arrays

Doing some minimal arithmetic operations on single variables is fairly straight-forward. Working with arrays is a wee bit more complex.

An array of numbers will be used for some examples:

#!/usr/bin/env bash
# bash math with arrays
clear
declare -a Binary # make Binary an array indexed by integers
# declare -A Read # make Read an associative array indexed by strings
Binary=(1 2 4 8 16 32 64 128)
numBinary=${#Binary[@]}
echo "There are $numBinary elements in 'Binary':"
for t in ${Binary[@]}; do
	echo $t
done

That should be familiar if you've read the Mathematics page. But what about dealing with the individual elements of an array.

Displaying the array can be done a couple of ways;

#!/usr/bin/env bash
# bash math with arrays
clear
declare -a Binary # make Binary an array indexed by integers
# declare -A Read # make Read an associative array indexed by strings
Binary=(1 2 4 8 16 32 64 128)
numBinary=${#Binary[@]}
echo "There are $numBinary elements in 'Binary':"
for t in ${Binary[@]}; do
	echo $t
done
echo
echo ${Binary[*]}

First we use a for loop to iterate through the array. Note that echo includes a newline character.

bashMath

There are 8 elements in 'Binary':
1
2
4
8
16
32
64
128

1 2 4 8 16 32 64 128

After that we used echo ${Binary[*]} to print the whole array. We used the asterisk for the index value, but the ampersand (@) also works here.

Note the different ways of display.

Now let's add some addition 😎:

#!/usr/bin/env bash
# bash math with arrays
clear
echo "bashMath"
echo
declare -a Binary # make Binary an array indexed by integers
# declare -A Read # make Read an associative array indexed by strings
Binary=(1 2 4 8 16 32 64 128)
numBinary=${#Binary[@]}
echo "There are $numBinary elements in 'Binary':"
for t in ${Binary[@]}; do
	echo $t
done
echo
echo ${Binary[*]}
echo
echo Addition
echo "Sum of first 3 elements 'expr'ed:"
Sum=`expr ${Binary[0]} + ${Binary[1]} + ${Binary[2]}`
echo $Sum

Here we used the expr command wrapped in back-ticks, assigning the result to the variable Sum.

expr is a tricky command to use, designed to be used inside of command substitution.

Another method of doing that operation is considered a better option.

#!/usr/bin/env bash
# bash math with arrays
clear
echo "bashMath"
echo
declare -a Binary # make Binary an array indexed by integers
# declare -A Read # make Read an associative array indexed by strings
Binary=(1 2 4 8 16 32 64 128)
numBinary=${#Binary[@]}
echo "There are $numBinary elements in 'Binary':"
for t in ${Binary[@]}; do
	echo $t
done
echo
echo ${Binary[*]}
echo
echo Addition
echo "Sum of first 3 elements 'expr'ed:"
Sum=`expr ${Binary[0]} + ${Binary[1]} + ${Binary[2]}`
echo $Sum
Sum=0
echo
echo "Sum of first 3 elements (better):"
Sum=$((Binary[0] + Binary[1] + Binary[2]))
echo $Sum

You can use whatever indices you want when operating on elements.

Finally we show how to get the sum of all the elements:

#!/usr/bin/env bash
# bash math with arrays
clear
echo "bashMath"
echo
Sum=0
declare -a Binary # make Binary an array indexed by integers
# declare -A Read # make Read an associative array indexed by strings
Binary=(1 2 4 8 16 32 64 128)
numBinary=${#Binary[@]}
echo "There are $numBinary elements in 'Binary':"
for t in ${Binary[@]}; do
	echo $t
done
echo
echo ${Binary[*]}
echo
echo Addition
echo "Sum of first 3 elements 'expr'ed:"
Sum=`expr ${Binary[0]} + ${Binary[1]} + ${Binary[2]}`
echo $Sum
Sum=0
echo
echo "Sum of first 3 elements (better):"
Sum=$((Binary[0] + Binary[1] + Binary[2]))
echo $Sum
Sum=0
echo Sum of all the elements:
for i in "${Binary[@]}"
do
	Sum=$((Sum + i))
done
echo $Sum
echo

bashMath

There are 8 elements in 'Binary':
1
2
4
8
16
32
64
128

1 2 4 8 16 32 64 128

Addition
Sum of first 3 elements 'expr'ed:
7

Sum of first 3 elements (better):
7
Sum of all the elements:
255

Arrays