CIS120 Linux Fundamentals by Scott Shaper

Arrays

Arrays help solve the problem of having to create separate variables for related data. Instead of creating variables like fruit1="apple", fruit2="banana", and so on, you can simply use one array to keep everything organized and accessible. This makes your scripts cleaner, more efficient, and easier to maintain.

The course setup.sh script creates ~/playground/chapter12. The examples below do not require any practice files; you can save and run your scripts in ~/playground/chapter12 to keep them with the other chapter 12 materials.

Whether you're managing a list of files to process, storing command results, or working with user inputs, arrays provide a powerful way to keep your data structured and your code elegant. Let's explore how these digital containers can transform the way you handle data in your scripts!

Quick Reference: Array Operations

Operation Description Common Use
array=("item1" "item2" "item3") Array declaration and initialization Creating a new array with predefined values
${array[index]} Access array element by index Retrieving a specific element from the array
array[index]="value" Assign value to array element Updating or adding elements at a specific position

When to Use Arrays

Practical Examples

Creating and initializing an array

In this example, we are creating an array called fruits and initializing it with four values: "apple", "banana", "cherry", and "date". Arrays in bash are created by assigning values in parentheses, with each value separated by spaces and optionally enclosed in quotes. The @ symbol is used to access all elements in the array.

#!/bin/bash
# Initialize the array
fruits=("apple" "banana" "cherry" "date")
echo "Initial fruits array: ${fruits[@]}"
Accessing a specific array element

In this example, we are accessing a specific element from the array using its index. Arrays in bash are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on. We use ${fruits[1]} to access the second element (banana).

#!/bin/bash
fruits=("apple" "banana" "cherry" "date")
# Print the second fruit
echo "The second fruit is: ${fruits[1]}"
Modifying an array element

In this example, we are modifying an existing element in the array by assigning a new value to a specific index. We replace the fourth fruit (at index 3) with "dragonfruit" using fruits[3]="dragonfruit".

#!/bin/bash
fruits=("apple" "banana" "cherry" "date")
# Replace the fourth fruit with 'dragonfruit'
fruits[3]="dragonfruit"
echo "After replacing the fourth fruit with dragonfruit: ${fruits[@]}"
Getting the array length

In this example, we are getting the number of elements in the array using ${#fruits[@]}. The # symbol before the array name gives us the length of the array, which tells us how many elements are stored in it.

#!/bin/bash
fruits=("apple" "banana" "cherry" "date")
# Print the number of fruits
echo "Number of fruits: ${#fruits[@]}"
Adding elements to an array

In this example, we are adding new elements to the end of the array using the += operator. This operator appends one or more elements to the existing array. We can add multiple elements at once by listing them in parentheses.

#!/bin/bash
fruits=("apple" "banana" "cherry" "date")
# Try to add two more fruits 
fruits+=("grape" "elderberry")
echo "After attempting to add 'grape' and 'elderberry': ${fruits[@]}"
echo "Number of fruits now: ${#fruits[@]}"
Iterating through array elements

In this example, we are looping through all elements in the array using a for loop. We use "${fruits[@]}" to access all elements, and the loop iterates through each element one at a time. The quotes around ${fruits[@]} are important to preserve spaces in array elements.

#!/bin/bash
fruits=("apple" "banana" "cherry" "date")
# Loop through the array and print a message
echo "Looping through fruits:"
for fruit in "${fruits[@]}"
do
  echo "I love $fruit"
done

Learning Aids

Tips for Success

Common Mistakes to Avoid

Best Practices