Working with Arrays in Shell Scripting: 3 Examples

Discover practical examples of working with arrays in shell scripting to enhance your coding skills.
By Taylor

Introduction to Arrays in Shell Scripting

Arrays in shell scripting allow you to store multiple values in a single variable, making it easier to manage data collections. They are particularly useful for tasks that involve lists or sequences, such as processing files or managing user inputs. In this article, we will explore three diverse examples of working with arrays in shell scripting that will help you understand their practical applications.

Example 1: Creating and Accessing an Array of Fruits

In this example, we’ll create an array containing a list of fruits and demonstrate how to access and print each fruit individually. This is useful for scenarios where you need to work with a collection of items.

# Create an array of fruits
fruits=(apple banana cherry date)

# Accessing and printing each fruit
for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done

This script initializes an array called fruits with four fruit names. The for loop iterates over the array, and each fruit is printed on a new line. You can easily modify the array to include more fruits or change the existing ones.

Notes: You can access individual elements by their index using ${fruits[0]}, ${fruits[1]}, and so on.

Example 2: Counting Elements in an Array

Here, we’ll create an array of numbers and count how many elements it contains. This is particularly useful in scenarios where you need to validate data or perform calculations based on the number of items.

# Create an array of numbers
numbers=(10 20 30 40 50)

# Count and print the number of elements
count=${#numbers[@]}
echo "The array contains $count elements."

In this example, we define an array called numbers with five integer values. The ${#numbers[@]} syntax calculates the number of elements in the array, which is then printed. This technique is handy when you need to know the size of your data before processing it further.

Notes: You can also use this method to dynamically check the size of arrays that may change during the execution of your script.

Example 3: Iterating Over an Array to Perform Operations

In this example, we will use an array of filenames and loop through each to perform a simple operation, like checking if the files exist. This is useful for file management tasks in shell scripts.

# Create an array of filenames
files=(file1.txt file2.txt file3.txt)

# Check if each file exists
for file in "${files[@]}"; do
    if [ -e "$file" ]; then
        echo "$file exists."
    else
        echo "$file does not exist."
    fi
done

This script initializes an array called files with three filenames. The for loop checks if each file exists using the -e flag. Depending on the result, it prints a message indicating whether the file exists or not. This example demonstrates a common use case for arrays in shell scripting, particularly when managing multiple files.

Notes: You can expand this example by adding more files or incorporating additional file operations, such as reading or writing content.

By exploring these examples of working with arrays in shell scripting, you can enhance your scripting capabilities and manage data collections more effectively.