# Using Arrays in Bash

Arrays in Bash are fundamental data structures that store multiple values in a single variable, offering a fast and efficient way to represent and manipulate related data as a group. Bash supports indexed arrays, which store ordered values referenced by a numeric index starting at `0`, and associative arrays, which store key-value pairs. This guide covers array syntax, creating and modifying arrays, using arrays with user input, looping through array elements, and matching array values against a regex pattern. By the end, you'll have scripts that build, modify, and iterate over both indexed and associative arrays, following Bash data-structure practices documented in [**Vultr Docs**](https://docs.vultr.com/how-to-use-arrays-in-bash).

---

## 1\. Learn the Array Syntax

Arrays in Bash use the `declare` command to initialize values based on the array type: a lowercase `a` declares an indexed array, while an uppercase `A` declares an associative array. Bash creates indexed arrays by default unless you use `declare` to specify otherwise. Mastering the [Bash case statement](https://docs.vultr.com/how-to-use-the-case-statement-in-bash) is also useful for handling the conditional logic that often works alongside arrays.

- Direct initialization:

  ```plaintext
  array=(element1 element2 element3)
  ```

  Enclosing values in parentheses `()` and separating them with spaces creates an indexed array that you can reference by the numeric index of each element.
- Indexed arrays:

  ```plaintext
  declare -a array
  ```
- Associative arrays:

  ```plaintext
  declare -A array
  ```
- Array reference syntax:

  ```plaintext
  ${array[index]}
  ```

  Reference an array element using curly braces `${}` and the element's index position or key.

## 2\. Create Arrays in a Script

**1\. Create a new array, such as** `new_array`**, using direct declaration and assign it the values** `1`**,** `2`**, and** `3`**:**

```console
new_array=(1 2 3)
```

**2\. Print all array elements:**

```console
echo ${new_array[@]}
```

Output:

```plaintext
1 2 3
```

**3\. Declare a new indexed array, such as** `index_array`**:**

```console
declare -a index_array
```

**4\. Assign** `value 1`**,** `value 2`**, and** `value 3` **to the array:**

```console
index_array[0]="value 1"

index_array[1]="value 2"

index_array[2]="value 3"
```

**5\. Print a single element using its index — for example, index** `1` **for** `value 2`**:**

```console
echo ${index_array[1]}
```

Output:

```plaintext
value 2
```

**6\. Declare a new associative array:**

```console
declare -A assoc_array
```

**7\. Assign** `string 1`**,** `string 2`**, and** `string 3` **to the array:**

```console
assoc_array[key1]="string 1"

assoc_array[key2]="string 2"

assoc_array[key3]="string 3"
```

**8\. Print a single element in the array:**

```console
echo ${assoc_array[key1]}
```

Output:

```plaintext
string 1
```

**9\. Print all elements in the associative array:**

```console
echo ${assoc_array[@]}
```

Output:

```plaintext
string 2 string 3 string 1
```

To handle multiple values effectively, see how to [use variables with arrays in Bash](https://docs.vultr.com/how-to-use-variables-in-bash#use-variables-with-arrays-in-bash) for more advanced scripting techniques.

## 3\. Manipulate Arrays in Bash

**1\. Create a new indexed array, such as** `new_index_array`**, with sample values:**

```console
new_index_array=("Apache" "Nginx" "MySQL" "PHP")
```

**2\. Print all elements in the array:**

```console
echo ${new_index_array[@]}
```

Output:

```plaintext
Apache Nginx MySQL PHP
```

**3\. Add new elements to the array using the** `+=` **operator:**

```console
new_index_array+=("Angular" "PostgreSQL")
```

**4\. Add a new element to the array at a specific index:**

```console
new_index_array[9]="Perl"
```

**5\. Modify an element in the array — for example, change** `PHP` **to** `PHP-FPM`**:**

```console
new_index_array[3]="PHP-FPM"
```

**6\. Delete an individual element using the** `unset` **command:**

```console
unset new_index_array[2]
```

**7\. Print all elements in the array:**

```console
echo ${new_index_array[@]}
```

Output:

```plaintext
Apache Nginx PHP-FPM Angular PostgreSQL Perl
```

**8\. Count the number of elements in the array:**

```console
echo ${#new_index_array[@]} 
```

Output:

```plaintext
6
```

The `#` prefix on the array name counts all elements currently in the array.

## 4\. Use Arrays with User Input

**1\. Create a new** `user_input.sh` **script:**

```console
nano user_input.sh
```

**2\. Add the following contents to the** `user_input.sh` **file:**

```bash
#!/bin/bash

echo "Hello, enter the values Apache Nginx MySQL separated by spaces:"
read -a user_array 

echo "You entered: ${user_array[@]}"
```

Save and close the file. The script prompts for input, stores each space-separated value as an element in the indexed `user_array`, and prints all of them with `echo`.

**3\. Run the script using Bash:**

```console
bash user_input.sh
```

Output:

```plaintext
Hello, enter the values Apache Nginx MySQL separated by spaces:
Apache Nginx MySQL
You entered: Apache Nginx MySQL
```

## 5\. Use Arrays with Loops in a Script

**1\. Create a new** `for-arrays.sh` **script:**

```console
nano for-arrays.sh
```

**2\. Add the following contents to the** `for-arrays.sh` **file:**

```bash
#!/bin/bash
new_index_array=("Apache" "Nginx" "MySQL" "PHP")

for value in ${new_index_array[@]}
do
        echo $value
done
```

Save and close the file. The `for` loop iterates through the array and prints each element on its own line.

**3\. Run the script using Bash:**

```console
bash for-arrays.sh
```

Output:

```plaintext
Apache
Nginx
MySQL
PHP
```

## 6\. Use Arrays with Regular Expressions

**1\. Create a new** `array_regex.sh` **script:**

```console
nano array_regex.sh
```

**2\. Add the following contents to the** `array_regex.sh` **file:**

```bash
#!/bin/bash

new_index_array=("Apache" "Nginx" "MySQL" "PHP")

pattern="^[anmpANMP].*"

for app in "${new_index_array[@]}"; do
    if [[ $app =~ $pattern ]]; then
        echo "$app can be installed on this system"
    fi
done
```

Save and exit the file. The script tests each array element against the `^[anmpANMP].*` regex pattern using an `if` statement.

**3\. Run the script using Bash:**

```console
bash array_regex.sh
```

Output:

```plaintext
Apache can be installed on this system
Nginx can be installed on this system
MySQL can be installed on this system
PHP can be installed on this system
```

## Next Steps

- Combine arrays with `for` or `while` loops to process larger datasets.
- Use associative arrays to model structured, key-based data in your scripts.
- Explore slicing and sorting array elements for more advanced data manipulation.
- Validate array elements with a regex pattern before using them in a script.

For the full guide with additional tips, visit the original article on [**Vultr Docs**](https://docs.vultr.com/how-to-use-arrays-in-bash).
