Introduction
Arrays let you store many values in one place. Indexed arrays use numbers to find each value easily.
array_name=(value1 value2 value3 ...)
# or
declare -a array_name
array_name=(value1 value2 value3 ...)fruits=(apple banana cherry)
echo ${fruits[0]}declare -a colors
colors=(red green blue)
echo ${colors[2]}empty_array=()
echo ${#empty_array[@]}numbers=(10) echo ${numbers[0]}
#!/bin/bash # Declare an indexed array of fruits fruits=(apple banana cherry) # Print all fruits echo "All fruits: ${fruits[@]}" # Print the first fruit echo "First fruit: ${fruits[0]}" # Add a new fruit fruits+=(date) # Print all fruits after adding echo "Fruits after adding one: ${fruits[@]}" # Print number of fruits echo "Number of fruits: ${#fruits[@]}"
fruits with values apple, banana, and cherry in Bash?numbers in Bash?colors=(red green blue), what will be the output of echo ${colors[1]}?myarray=(one two three four five six seven eight nine ten
days with the values Mon, Tue, and Wed, then print the total number of elements in the array?Pass the test and this pattern moves unknown