Tutorials  /  Linux Basics

Arrays in Shell Scripts: Optimizing Data Storage and Access

Ccentron Redaktion · October 2024 ·3 min read ·Linux Basics, Tutorial

Discover the power of arrays in shell scripts in our latest blog post! Learn how to efficiently process and optimize large datasets. Dive into the world of shell programming and maximize your productivity!

What are Arrays?

Arrays are data structures that allow you to store a variety of elements in a single variable. While simple variables can only hold a single data element, arrays allow for the storage of an unlimited number of elements. This is particularly useful when processing large amounts of data, as arrays are more efficient than simple variables.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Creating Arrays in Shell Scripts

In shell scripts, there are two types of arrays:

  • Indexed Arrays: Store elements with an index starting at 0.
  • Associative Arrays: Store elements in key-value pairs.

The default array created is an indexed array. If you specify index names, it becomes an associative array, and the elements can be accessed via index names instead of numbers.

To declare arrays, use the declare statement:

Code
declare -A assoc_array  # Declaration of an associative array
assoc_array[key]=value   # Adding elements to the associative array

declare -a indexed_array  # Declaration of an indexed array
indexed_array[0]=value     # Adding elements to the indexed array

Working with Arrays in Shell Scripts

Now that you know how to create arrays, it is important to understand how to work with them. Here are some basic operations:

1. Accessing Array Elements Individually:

Array elements can be accessed individually by their index or key.

Console
echo ${assoc_array[element1]}  # Accessing an element in an associative array
echo ${index_array[0]}          # Accessing an element in an indexed array

2. Sequentially Reading Array Elements:

You can iterate through all elements of an array sequentially with a loop.

Code
for i in ${index_array[@]}
do
    echo $i
done

Built-in Operations for Arrays in Shell Scripts

In addition to basic operations, the shell also provides built-in functions for working with arrays:

1. Accessing All Array Elements:

Use ${array[@]} to retrieve all elements of an array.

Console
echo ${assoc_array[@]}    # Prints all elements of the associative array

2. Counting the Number of Elements in an Array:

Use ${#array[@]} to count the number of elements in an array.

Console
echo ${#index_array[@]}   # Outputs the number of elements in the indexed array

3. Deleting Individual Array Elements:

Use unset to delete specific array elements.

Code
unset index_array[1]      # Deletes the second element in the indexed array

Conclusion

Arrays are an important concept in shell programming that allows for efficient data management and processing. With the basics presented here, you should be able to effectively utilize arrays in your shell scripts. If you have questions or need further information, feel free to leave a comment!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren