Bash arrays allow convenient handling of related data in a single variable, which helps keep scripts organized and efficient. Arrays can store multiple values of various types, from strings to integers, and simplify data manipulation in complex automation tasks. They offer slicing, iteration, and expansion features that streamline everyday scripting processes.

Indexed arrays use numeric indices to reference elements and require minimal setup when structuring a collection of values. Associative arrays rely on string-based keys, allowing descriptive mappings that improve readability and context within scripts. Both types leverage essential tools like parameter expansion and unset, which support flexible operations for a wide range of scenarios.

Recent versions of Bash (4.0 and later) include full support for both array types. This ensures robust data handling capabilities in modern scripting environments, whether working with system logs, processing text files, or maintaining modular configuration scripts. Arrays thereby serve as a cornerstone of reliable and maintainable Bash automation.

Steps to manage indexed arrays:

Indexed arrays in Bash rely on zero-based numerical indices and provide straightforward ways to store and retrieve multiple values. They are ideal for iterative tasks, command-line argument processing, or grouping a fixed series of elements in a logical sequence.

  1. Declare an indexed array using the declare builtin with the -a option.
    $ declare -a fruits=("apple" "banana" "cherry")
    $ echo "${fruits[@]}"
    apple banana cherry
  2. Access a single element by referencing its index in square brackets.
    $ echo "${fruits[1]}"
    banana
  3. Append a value to the array using the += operator.
    $ fruits+=("orange")
    $ echo "${fruits[@]}"
    apple banana cherry orange
  4. Modify an element by assigning a new value to a specific index.
    $ fruits[2]="blueberry"
    $ echo "${fruits[2]}"
    blueberry
  5. Remove a single element or the entire array using the unset builtin.
    $ unset fruits[1]
    $ echo "${fruits[@]}"
    apple blueberry orange
  6. Retrieve the number of elements with the special syntax ${#array_name[@]}.
    $ echo "${#fruits[@]}"
    3
  7. Use an iteration structure to process each element if needed.
    $ for fruit in "${fruits[@]}"; do
    >   echo "Fruit: $fruit"
    > done
    Fruit: apple
    Fruit: blueberry
    Fruit: orange

    Iterate through elements to handle them programmatically, such as processing files listed in an array or passing values to another command.

Steps to manage associative arrays:

Associative arrays in Bash use string keys, making them useful for storing data with descriptive identifiers. They require Bash version 4 or higher and can streamline tasks where keys must provide meaningful context.

  1. Declare an associative array using the declare builtin with the -A option.
    $ declare -A capital
  2. Assign a value by using a string-based key in square brackets.
    $ capital["France"]="Paris"
    $ echo "${capital["France"]}"
    Paris
  3. Modify or add elements by reassigning the desired key.
    $ capital["Spain"]="Madrid"
    $ echo "${capital["Spain"]}"
    Madrid
  4. Remove an element or the entire array using unset.
    $ unset capital["Spain"]
    $ echo "${capital["Spain"]}"

    Keys not present in the array return empty values when queried, as seen above.

  5. Loop through keys by expanding the @ or * array specifier in conjunction with a standard for loop.
    $ for country in "${!capital[@]}"; do
    >   echo "$country -> ${capital[$country]}"
    > done
    France -> Paris
  6. Retrieve the total number of elements using ${#array_name[@]}.
    $ echo "${#capital[@]}"
    1
  7. Check your Bash version if associative arrays are not recognized.
    $ echo "$BASH_VERSION"
    5.1.4(1)-release

    Upgrade to Bash 4 or higher if associative arrays are required but unavailable in your current environment.

Discuss the article:

Comment anonymously. Login not required.