Creating an array and initializing it

You will now learn about creating an array in the Bash shell.

If the array name is FRUIT, then we can create an array, as follows:

FRUIT[index]=value

Index is the integer value. It should be 0 or any positive integer value.

We can also create an array, as follows:

$ declare -a array_name$ declare -a arrayname=(value1 value2 value3)

This is an example:

$ declare -a fruit=('Mango' 'Banana' 'Apple' 'Orange' 'Papaya')$ declare -a array_name=(word1 word2 word3 ...)$ declare -a fruit=( Pears Apple Mango Banana Papaya )$ echo ${fruit[0]}Pears$ echo ${fruit[1]}Apple$ echo "All the fruits are ${fruit[*]}"    All the fruits are Pears Apple Mango Banana Papaya$ echo "The number of elements in the array are ${#fruit[*]}" ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.