Accueil > Système, Tutoriel > Using Bash Arrays with Examples

Using Bash Arrays with Examples

12/08/2021 Categories: Système, Tutoriel Tags: , , ,
Print Friendly, PDF & Email

bash-scripting-32-638Arrays can be a useful tool when coding your bash scripts.  The simplest way that I can define an array is to state that an array is a variable for a multi-instance dataset.

For example, a variable is used when there is a single value from a dataset like the IP Address of a server.  However, an array can be used to store all of the IP Addresses in your server room.

Speaking of IP Addresses and bash arrays, my last article (Detect and Block WordPress Brute Force Login Attacks) includes a script which is an example of how an array can be used in bash scripting.

Because arrays can be so useful in bash scripting, I thought that I would put together the following article detailing ways of Using Bash Arrays with Examples.

Initializing Bash Arrays or Assigning Values to Arrays

For arrays to be useful, we need to be able to assign values to them.  We assign values to an array by listing the array along with its instance number as shown below.  This method will assign each instance of the array one by one.

#!/bin/bash
myarray[0]=Hello
myarray[1]=World,
myarray[3]=Happy
myarray[4]=Friday

# Display all instances of the array
echo ${myarray[*]}

We can see above that in addition to being able to assign the values one by one, we can reference all array instances with an asterisk (*).  Another way to display all instances of the array is to use the following “echo ${myarray[@]}”

We run the script and get:

$ ./arrays.sh
Hello World, Happy Friday

We can also retrieve individual instances of an array by specifying the individual array instance number.  We modify the above script slightly to retrieve a couple of the instances.

#!/bin/bash
myarray[0]=Hello
myarray[1]=World,
myarray[3]=Happy
myarray[4]=Friday

# Display all instances of the array
echo ${myarray[0]} ${myarray[4]}

We run the script again and we get:

$ ./arrays.sh
Hello Friday

Other Ways to Populate a Bash Array

White space is a delimiter when assigning values to an array.  This means that the example:

myarray=(Hello World)

will assign values to both instances 0 and 1 of the array “myarray” in one statement.

Lire aussi:  Tutorial: Using VMWare ESXi and PFsense as a network firewall/router

below, rr=(Hello World)

Populate Bash Array From Command Output

A very common method for populating a full array is by capturing the output from a bash command into the array.  Below is an example from my WordPress brute force script that I mentioned above.  The array named “IPADDRESSES” is populated

IPADDRESSES=(`/bin/grep wp-login.php ${TMPLOG} | /bin/awk '{print $1}' | sort | uniq -c | sort -rn | awk '{printf "%s:%s\n", $1, $2}'`)

Here are a few other things that you can do with a bash array

Show all array instance control variables for an array.

#!/bin/bash
myarray=(Hello World, happy friday)

echo ${!myarray[*]}

which when run gives us:

$ ./arrays.sh
0 1 2 3

This has uses in a loop environment perhaps as a control variable for a for loop.

Show How Many Instances There are in Your Bash Array

Sometimes you need to know how many instances there are in your bash array,

#!/bin/bash
myarray=(Hello World, happy friday)

echo ${#myarray[*]}

which when run gives us:

$ ./arrays.sh
4

The 4 above represents an instance for each word:  “Hello World, happy friday”

Display the Length of a Bash Array Instance

Sometimes you want to get the length of the text in each of your bash array instances.

#!/bin/bash
array=(Hello World, happy friday Supercalifragilisticexpialidocious )
echo ${#array[0]}
echo ${#array[1]}
echo ${#array[2]}
echo ${#array[3]}
echo ${#array[4]}

which when run looks like this:

$ ./arrays.sh
5
6
5
6
34

Each row above represents the length of the word in each bash array instance.

Source: uptimemadeeasy.com

Categories: Système, Tutoriel Tags: , , ,
Les commentaires sont fermés.