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.

  1. Arrays in Bash

1.1. Defining an Array

In Bash, you can define an array using parentheses () and separating the elements with spaces.

# Define an array
fruits=("apple" "banana" "cherry")

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

1.3. Adding Elements to an Array

You can add elements to an array by specifying the index.

# Add an element to the array
fruits[3]="date"
echo ${fruits[3]}  # Output: date

1.4. Looping Through an Array

You can loop through an array using a for loop.

# Loop through the array
for fruit in "${fruits[@]}"; do
  echo $fruit
done

1.5. Array Length

You can get the length of an array using the ${#array[@]} syntax.

# Get the length of the array
echo ${#fruits[@]}  # Output: 4

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

  1. 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 York

2.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]}"
done

2.4. Associative Array Length

You can get the length of an associative array using the ${#array[@]} syntax.

# Get the length of the associative array
echo ${#person[@]}  # Output: 3

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]}"
done

Conclusion

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.

© Copyright 2024. All rights reserved