Welcome fellow bash scripter! Looking to step up your scripting game by leveraging bash arrays? You‘ve come to the right place.
When I first started out with shell scripting, I mostly relied on plain old regular variables to store data. But once I discovered the power of bash arrays, the possibilities exploded.
Today I want to take you on a deep dive into arrays. By the end, you‘ll have a comprehensive overview of:
- What problem arrays solve
- The ins and outs of array syntax
- Powerful techniques for manipulating array data
- Built-in array functions that make life easier
- Real-world examples that cement the concepts
I‘ll share plenty of annotated code snippets you can try out live as we go along. So get ready to level up your bash skills!
What Are Bash Arrays?
Arrays let you store collections of data, similar to lists or dictionaries in other languages.
The two main types are:
Indexed arrays – Store data where each element can be accessed by its numerical index. They maintain an ordering of elements.
Associative arrays – Store data as key-value pairs, like Python dictionaries. The key acts as the index to retrieve associated values.
Compared to regular variables, arrays empower you to:
- Store many elements together instead of just one value
- Access elements dynamically by index or key
- Modify contents with operations like append, insert, remove
- Build complex logic by iterating arrays in loops
In short: Arrays supercharge bash variables with array-specific syntax and functions.
Let‘s look at how to put them into action!
Declaring Arrays in Bash
Declaring an array initializes the data structure and allows you to populate it.
Standard Indexed Arrays
Here is syntax for declaring an indexed array:
fruits=(apple orange banana)
This creates an array called fruits
with three elements populated.
We can verify it worked:
echo ${fruits[1]} # Access 2nd element (0 index)
# Prints orange
The curly bracket syntax lets us retrieve a specific element by its index number.
You can also declare an empty array upfront then append values as needed:
veggies=()
veggies+=("carrots")
veggies+=("celery")
The += operator appends each new element.
Associative Arrays
Associative or dictionary-style arrays work a bit differently since they store key-value pairs.
declare -A hexcolors
hexcolors[red]="#ff0000"
hexcolors[green]="#00ff00"
hexcolors[blue]="#0000ff"
Now you can access elements via their key:
echo ${hexcolors[green]} # Prints 00ff00
The main decision around arrays is whether to use indexing or associative storage for your use case.
Accessing and Looping Array Elements
Accessing specific array elements for either indexing or key lookup is straightforward. But the real power comes from iterating all values which unlocks bulk processing.
There are a few approaches for iteration such as for
loops or parameter expansion.
Indexed Array Looping
Use parameter expansion to enable looping:
fruits=(apple orange banana)
for i in "${fruits[@]}"
do
echo $i
done
# Prints each fruit on a new line
The ${fruits[@]}
syntax expands the array so we can iterate it.
Associative Array Looping
The same expansion technique works for associative arrays:
for color in "${hexcolors[@]}"
do
echo $color
done
# Prints values: ff0000 00ff00 0000ff
And to loop the keys:
for key in "${!hexcolors[@]}"
do
echo $key
done
# Prints keys: red green blue
The ${!array[@]}
syntax grabs all the keys.
This makes it easy to operate on all elements.
Modifying Array Contents
Beyond accessing existing values, directly modifying array contents unlocks additional capabilities.
Appending New Elements
Adding data is a common need. Here is an example of append:
fruits+=("strawberry")
We can also add multiple elements:
veggies+=("onions" "spinach")
For associative arrays, append by assigning to a new key:
hexcolors[periwinkle]="#CCCCFF"
Inserting and Removing
We may want to insert items at specific positions.
Insert by setting explicit indexes:
fruits[2]="kiwi"
This shifts other elements forward one position.
To remove, unset
the target index:
unset fruits[2]
This removes index 2, shifting everything back one spot.
As we can see, arrays have built-in data manipulation capabilities.
Built-in Array Functions
There are also syntax constructs that help transform arrays:
Get total elements:
Determine array size via:
size=${#fruits[@]}
Gives the total number of elements for breadth checks.
Sort elements:
Sort an array alphanumeric:
sorted=($(for i in "${fruits[@]}"; do echo $i; done | sort))
Reverse order:
Flip order using a reverse for loop:
reversed=($(for ((i=${#fruits[@]}-1; i>=0; i--)); do echo ${fruits[$i]}; done))
Concatenate arrays:
Joining arrays can create aggregations:
combined=(${fruits[@]} ${veggies[@]})
These give a taste of built-in manipulations possible.
Real-World Array Use Cases
With data structures basics covered, where do we apply this knowledge?
Arrays shine for tasks like:
Store command output
Capture terminal output directly into an array for processing:
files=($(ls))
Read file data
Populate via file contents line-by-line:
while read line; do
lines+=("$line");
done < "data.txt"
Data file parsing
Associative arrays allow key=value parsing:
declare -A config
while read line; do
key=$(echo $line | cut -d= -f1)
val=$(echo $line | cut -d= -f2)
config[$key]=$val
done < "config.conf"
Function argument
Pass arrays between functions:
getData() {
local result=("$@")
# Use $result array
# ...
}
myData=("a" "b" "c")
getData "${myData[@]}"
These examples demonstrate flexible use for scripting.
Let‘s recap what we covered…
Bash Array Key Takeaways
We dug deep into common array operations today. Here are the key points:
- Arrays hold multiple data in indexed or associative structure
- Declare arrays, then append values or assign key-value pairs
- Access elements directly via index or key names
- Built-in modifiers like append, insert, and unset
- Available functions for sorting, reversing, concatenation
- Integrate arrays into loops, commands, and custom functions
There‘s so much more that can be done combining arrays with other script building blocks. I encourage you to try out examples locally and think about use cases that could apply to your own projects.
Happy array scripting my friend!