To declare an array variable in shell scripting, you can use the declare command with the -a option, or you can simply assign values to an array variable using parentheses. Here are two common methods:
Method 1: Using declare
declare -a myArray
myArray=(1 2 3 4 5)
Method 2: Direct Assignment
myArray=(1 2 3 4 5)
Accessing Array Elements
You can access elements of the array using the following syntax:
echo ${myArray[0]} # Outputs: 1
echo ${myArray[1]} # Outputs: 2
Example of Declaring and Using an Array
#!/bin/bash
# Declare an array
declare -a fruits=("Apple" "Banana" "Cherry")
# Access and print elements
echo ${fruits[0]} # Outputs: Apple
echo ${fruits[1]} # Outputs: Banana
You can choose either method to declare an array based on your preference.
