07. Bash Arrays¶
In Bash scripting, an array is a variable that can hold multiple values at the same time. Instead of storing a single value like a normal variable, arrays allow you to store a list of values and access them using an index.
Bash arrays are zero-indexed, meaning the first element starts at index 0.
Example concept:
1. Declaring a Simple Bash Array¶
A Bash array can be declared by placing values inside parentheses.
Example Script¶
#!/bin/bash
# Declare array with elements
ARRAY=('Debian Linux' 'Redhat Linux' Ubuntu Linux)
# Get number of elements in array
ELEMENTS=${#ARRAY[@]}
# Loop through array and print elements
for (( i=0;i<$ELEMENTS;i++)); do
echo ${ARRAY[${i}]}
done
Explanation¶
1. Declaring the Array¶
This creates an array named ARRAY with four elements:
| Index | Value |
|---|---|
| 0 | Debian Linux |
| 1 | Redhat Linux |
| 2 | Ubuntu |
| 3 | Linux |
Note:
The first two values are wrapped in quotes because they contain spaces.
2. Getting the Number of Elements¶
${#ARRAY[@]} returns the total number of elements in the array.
Example output:
3. Accessing Array Elements¶
Example:
4. Looping Through the Array¶
This loop:
-
Starts at index
0 -
Runs until the last element
-
Prints each array value
Output¶
2. Reading File Content into a Bash Array¶
Instead of defining array values inside the script, Bash can read values from a file and store them in an array.
Example Script¶
#!/bin/bash
# Declare array
declare -a ARRAY
# Link file descriptor 10 with stdin
exec 10<&0
# Replace stdin with file supplied as first argument
exec < $1
let count=0
while read LINE; do
ARRAY[$count]=$LINE
((count++))
done
echo Number of elements: ${#ARRAY[@]}
# Print array contents
echo ${ARRAY[@]}
# Restore stdin
exec 0<&10 10<&-
Example Input File¶
bash.txt
Running the Script¶
Output¶
Explanation¶
1. Declaring an Array¶
This explicitly declares a variable as an array.
2. Redirecting Input from File¶
This replaces standard input (stdin) with the file provided as the first argument.
Example:
Here $1 = bash.txt
3. Reading File Line by Line¶
This reads each line of the file one by one.
4. Storing Lines into the Array¶
Each line from the file becomes an array element.
Example:
| Index | Value |
|---|---|
| 0 | Bash |
| 1 | Scripting |
| 2 | Tutorial |
| 3 | Guide |
5. Incrementing the Index¶
This increases the array index after each iteration.
6. Printing Number of Elements¶
Returns total array elements.
Output:
7. Printing Array Content¶
${ARRAY[@]} prints all elements of the array.
Output:
Key Bash Array Syntax¶
| Syntax | Description |
|---|---|
ARRAY=(a b c) |
Declare array |
${ARRAY[0]} |
Access element |
${ARRAY[@]} |
All elements |
${#ARRAY[@]} |
Number of elements |
ARRAY[3]=value |
Assign element |
unset ARRAY[2] |
Remove element |
Summary¶
Bash arrays allow scripts to manage multiple values efficiently. Key capabilities include:
-
Declaring arrays
-
Accessing elements using indexes
-
Looping through arrays
-
Reading external data (such as files) into arrays
Arrays are commonly used in Bash scripts for:
-
Handling wordlists
-
Processing file data
-
Automating system tasks
-
Managing command outputs
Understanding arrays is essential for writing more structured and powerful Bash scripts.