Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


These examples are all scripts (files)
and NOT meant to be run on the command line.
Read the Scripting page before continuing.

Arrays


Arrays are a common data type in most programming languages. As you might expect, an array refers to a collection of things. In the real world we deal with arrays all the time:

In a computer environment we also have lots of collections:

We can deal with all of these collections (and their elements) using arrays.

A very powerful thing to understand.

Defining Arrays

Defining, or assigning values, to an array can be done in several ways.

Sometimes we don't know ahead of time what the elements of an array are going to be, or how big the array is going to be.

Unknown

In that case we define it thus:

NewStuff=() # this also empties the array
and assign values to it:
NewStuff[0]="peppers"
NewStuff[1]="onions"
NewStuff[2]="more money"
printf '%s\n' "${NewStuff[@]}"

We added double quotes around '${NewStuff[@]}' because [2] contains a space.

To replace an item:

NewStuff[2]="Forget This Item"
echo ${NewStuff[2]}
Forget This Item

Known

If you DO know what you want to put in the array already, it's done simply:

declare -a SQLBooks=(
"Definitive Guide to SQLite 2E (The)"
"Practical SQL Handbook 4E (The)"
"Joe Celko's SQL For Smarties: Advanced SQL Programming 2E"
"Joe Celko's Data & Databases: Concepts In Practice"
"Database Design for Mere Mortals"
"SQL Queries for Mere Mortals"
"Using SQLite"
"Joe Celko's SQL Puzzles and Answers 2E"
)

Since the titles have spaces, we need to double-quote them.

Viewing the whole array:

for i in ${SQLBooks[@]} do
	echo $i
done

To see how many elements are in an array is done using the octothorpe (#) like this:

${#SQLBooks[@]}
.. and used like this:
echo There are ${#SQLBooks[@]} SQL books
There are 8 SQL books

echo There are ${#SQLBooks[*]} SQL books
There are 8 SQL books

Note we can use either an at sign or asterisk as the index value.

They indicate we are referring to all the elements.

There is a subtle but important difference in when to use one or the other:
The bash man page says

If the word is double-quoted, ${ name [*]} expands to a single word with the value of each array member separated by the first character of the IFS variable, and ${ name [@]} expands each element of name to a separate word.

In English, each title in the above example of SQL books was double-quoted, so when we printed the array, each title was treated as a whole.

Even better, we can assign that result to a variable:

NumBooks=${#SQLBooks[@]}
echo $NumBooks
8

Array Looping

The way we typically refer to a single element of an array is to use a sequential, numerical index.

Array indexes start at 0, not 1
and MUST be unique

To refer to an element of an array we use the index in brackets:

echo ${SQLBooks[4]}
Database Design for Mere Mortals

We can loop through an array by using a variable as an index, similar to a loop in other programming languages.

for i in "${SQLBooks[@]}"; do
	echo "$i"
done
Definitive Guide to SQLite 2E (The)
Practical SQL Handbook 4E (The)
Joe Celko's SQL For Smarties: Advanced SQL Programming 2E
Joe Celko's Data & Databases: Concepts In Practice
Database Design for Mere Mortals
SQL Queries for Mere Mortals
Using SQLite
Joe Celko's SQL Puzzles and Answers 2E

Note the double-quotes in the initial use of the array name.

Using Arrays

In bash, we need 2 types of brackets: square and curly, and we prefix the name of the array with a dollar sign ‘ $ ‘ as in

${Books[4]} or ${Trucks[34]}

The length of an array (the number of elements) is found by referring to ${array[@]} as in:

lengthTrucks = ${#Trucks[@]}

So to see how many friends we have, we would do:

echo "I have "${#MyFriends[@]}" friends!"
I have 3 friends!

Hopefully that number will increase.

To see a particular element in the array, we use the index of that element:

echo My 3rd friend is ${MyFriends[2]}
My 3rd friend is Jean Michel

Defining Arrays 2

If the array is going to be small, and we know all the elements included, we can define it this way:

MyFriends=([2]="Billy Jo" [0]=Alex [1]=Harvey)
echo My 2nd friend is ${MyFriends[1]}
My 2nd friend is Harvey

Note that we don't have to assign indexes in the correct sequential order, as long as they are unique.

Another way to populate an array is using mapfile.

Sort Arrays | Array Subsets | Math with Arrays | Loops