
Arrays in Bash are fundamental data structures that store multiple values in a single variable. Arrays offer fast and efficient methods to represent and manipulate related data as a single group. Bash supports two types of arrays:
0.This article explains how to use arrays in Bash to store and reference data. Mastering the Bash case statement is crucial for handling conditional logic in scripts efficiently.
Arrays in Bash use the declare command to initialize values based on the array type. The lowercase character a declares an indexed array while an uppercase A declares an associative array. Bash supports indexed arrays by default unless you use the declare command.
Direct initialization:
Initializing encloses an array in brackets () and separates values with a space. Direct initialization creates an indexed array you can reference using the numeric index of each element.
Indexed arrays:
Associative arrays:
Array reference syntax.
You can reference Arrays in Bash using the curl braces ${} and specify an index position to access an element.
To create arrays in Bash, use variables and apply one or more elements to the array using the equals sign =. Follow the steps below to create arrays in Bash and retrieve values based on the index position or key.
Create a new array, such as new_array using the direct declaration method and assign it the 1,2, and 3 values.
Print all array elements.
Output:
Declare a new indexed array, such as index_array.
Assign value 1, value 2, and value 3 values to the array.
Print a single element in the array using its index. For example, for value 2, use an index of 1.
Output:
Declare a new associative array.
Assign string 1, string 2, and string 3 values to the array.
Print a single element in the array.
Output:
Print all elements in the associative array.
Output:
To handle multiple values effectively, learn how to use variables with arrays in Bash for efficient data storage. Check out our in-depth guide on how to use variables with arrays in Bash for more advanced scripting techniques.
You can manipulate arrays in Bash to lists, add, or remove new elements without affecting the array structure. This is important when using arrays with loops or conditional statements to set new values for elements. Follow the steps below to create an indexed array and modify its elements in Bash.
Create a new indexed array, such as my_index_array and set Apache, Nginx, MySQL, and PHP as sample values.
Print all elements in the array.
Output:
Add new elements to the array using the += operator.
Add a new element to the array and specify its index.
Modify an element in the array. For example, change PHP to PHP-FPM.
Delete individual elements in the array using the unset command.
Print all elements in the array.
Output:
Count the number of elements in the array.
Output:
The # prefix in the array name counts all elements.
You can use arrays to handle user input and store multiple values as elements. Each space in the user input creates a new element in an array that you can reference using the element's index or key. Follow the steps below to create a Bash script that uses the read command to store new array elements based on the user's input.
Create a new user_input.sh script.
Add the following contents to the user_input.sh file.
Save and close the file.
The above Bash script prompts a user to input values and stores each value in an indexed input_array array before outputting all elements using the echo command.
Run the script using Bash.
Output:
You can iterate through array elements with loops such as the for loop and continuously execute specific commands or actions. Follow the steps below to create a new Bash script that iterates through an array and prints each element on a new line.
Create a new for-arrays.sh script.
Enter the following contents to the for-arrays.sh file.
Save and close the file.
The above Bash script uses a for loop to iterate through an array and prints each element on a new line.
Run the script using Bash.
Output:
You can use arrays with regular expressions (regex) to match patterns against multiple elements in an array. Follow the steps below to create a bash script that uses an indexed array of strings and a regex pattern to iterate through all elements using a for loop.
Create a new array_regex.sh script.
Add the following contents to the array_regex.sh file.
Save and exit the file.
The above Bash script creates a new variable with a list of strings and uses the regex pattern ^[].* to test each element using the if conditional statement.
Run the script using Bash.
Output:
You have used arrays in Bash to store data and perform specific operations to retrieve or manipulate elements. Arrays store multiple values and allow you to integrate other Bash operations, such as functions, loops, or conditional statements to execute specific commands or reference data.
0 Comments
Be the first to comment and share your perspective with the community.