In this section, we will explore arrays and associative arrays in Bash. Arrays are a fundamental data structure that allows you to store multiple values in a single variable. Associative arrays, on the other hand, allow you to use strings as keys, providing a way to create key-value pairs.
- Arrays in Bash
1.1. Defining an Array
In Bash, you can define an array using parentheses () and separating the elements with spaces.
1.2. Accessing Array Elements
You can access array elements using the index, starting from 0.
# Access the first element
echo ${fruits[0]} # Output: apple
# Access the second element
echo ${fruits[1]} # Output: banana1.3. Adding Elements to an Array
You can add elements to an array by specifying the index.
1.4. Looping Through an Array
You can loop through an array using a for loop.
1.5. Array Length
You can get the length of an array using the ${#array[@]} syntax.
Practical Exercise: Arrays
Exercise: Create an array of your favorite colors and print each color using a loop.
Solution:
# Define an array of favorite colors
colors=("red" "blue" "green" "yellow")
# Loop through the array and print each color
for color in "${colors[@]}"; do
echo $color
done
- Associative Arrays in Bash
2.1. Defining an Associative Array
Associative arrays are declared using the declare keyword with the -A option.
# Declare an associative array declare -A person # Add key-value pairs to the associative array person[name]="John" person[age]=30 person[city]="New York"
2.2. Accessing Associative Array Elements
You can access elements using the key.
# Access elements using keys
echo ${person[name]} # Output: John
echo ${person[age]} # Output: 30
echo ${person[city]} # Output: New York2.3. Looping Through an Associative Array
You can loop through the keys of an associative array using a for loop.
# Loop through the keys of the associative array
for key in "${!person[@]}"; do
echo "$key: ${person[$key]}"
done2.4. Associative Array Length
You can get the length of an associative array using the ${#array[@]} syntax.
Practical Exercise: Associative Arrays
Exercise: Create an associative array to store information about a book (title, author, year) and print each piece of information.
Solution:
# Declare an associative array for a book
declare -A book
# Add key-value pairs to the associative array
book[title]="1984"
book[author]="George Orwell"
book[year]=1949
# Loop through the keys and print each piece of information
for key in "${!book[@]}"; do
echo "$key: ${book[$key]}"
doneConclusion
In this section, we covered the basics of arrays and associative arrays in Bash. We learned how to define, access, and manipulate arrays, as well as how to loop through them. Associative arrays provide a powerful way to handle key-value pairs, making your scripts more flexible and efficient. In the next section, we will delve into string manipulation techniques in Bash.
Bash Programming Course
Module 1: Introduction to Bash
Module 2: Basic Bash Commands
- File and Directory Operations
- Text Processing Commands
- File Permissions and Ownership
- Redirection and Piping
