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.

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 is also useful for handling the conditional logic that often works alongside arrays.

  • Direct initialization:

    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:

    declare -a array 
  • Associative arrays:

    declare -A array 
  • Array reference syntax:

    ${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:

$ new_array=(1 2 3) 

2. Print all array elements:

$ echo ${new_array[@]} 

Output:

1 2 3 

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

$ declare -a index_array 

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

$ 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:

$ echo ${index_array[1]} 

Output:

value 2 

6. Declare a new associative array:

$ declare -A assoc_array 

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

$ assoc_array[key1]="string 1" $ assoc_array[key2]="string 2" $ assoc_array[key3]="string 3" 

8. Print a single element in the array:

$ echo ${assoc_array[key1]} 

Output:

string 1 

9. Print all elements in the associative array:

$ echo ${assoc_array[@]} 

Output:

string 2 string 3 string 1 

To handle multiple values effectively, see how to 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:

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

2. Print all elements in the array:

$ echo ${new_index_array[@]} 

Output:

Apache Nginx MySQL PHP 

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

$ new_index_array+=("Angular" "PostgreSQL") 

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

$ new_index_array[9]="Perl" 

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

$ new_index_array[3]="PHP-FPM" 

6. Delete an individual element using the unset command:

$ unset new_index_array[2] 

7. Print all elements in the array:

$ echo ${new_index_array[@]} 

Output:

Apache Nginx PHP-FPM Angular PostgreSQL Perl 

8. Count the number of elements in the array:

$ echo ${#new_index_array[@]} 

Output:

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:

$ nano user_input.sh 

2. Add the following contents to the user_input.sh file:

#!/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:

$ bash user_input.sh 

Output:

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:

$ nano for-arrays.sh 

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

#!/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:

$ bash for-arrays.sh 

Output:

Apache Nginx MySQL PHP 

6. Use Arrays with Regular Expressions

1. Create a new array_regex.sh script:

$ nano array_regex.sh 

2. Add the following contents to the array_regex.sh file:

#!/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:

$ bash array_regex.sh 

Output:

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.