In shell scripting, arrays can be declared and initialized in a couple of ways. Here are the common methods:
-
Declaring and Initializing an Array:
You can declare and initialize an array in one line using parentheses:my_array=(value1 value2 value3) -
Declaring an Empty Array:
You can declare an empty array and then add elements later:my_array=() # Declare an empty array -
Adding Elements to an Empty Array:
You can add elements to the array using the+=operator:my_array+=("value1") my_array+=("value2") -
Declaring an Array with Explicit Indices:
You can also declare an array by specifying indices:my_array[0]="value1" my_array[1]="value2"
These methods allow you to create and initialize arrays in shell scripts effectively.
