-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_script.sh
More file actions
24 lines (20 loc) · 754 Bytes
/
array_script.sh
File metadata and controls
24 lines (20 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
# Declare an array with at least 5 elements
my_array=("apple" "42" "banana" "23" "cherry")
my_array[5]="GTA"
# a. Access and display individual elements of the array by their index
echo "Element at index 0: ${my_array[0]}"
echo "Element at index 1: ${my_array[1]}"
echo "Element at index 2: ${my_array[2]}"
echo "Element at index 3: ${my_array[3]}"
echo "Element at index 4: ${my_array[4]}"
echo "Element at index 5 ${my_array[5]}"
echo
# b. Print the entire array
echo "Entire array: ${my_array[*]}"
echo
# c. Use a loop to iterate through all the elements and print each element with its corresponding index
echo "Array elements with their indices:"
for index in "${!my_array[@]}"; do
echo "Index $index: ${my_array[$index]}"
done