官术网_书友最值得收藏!

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[*]}"
The number of elements in the array are 5
$ unset fruit
or
$ unset ${fruit[*]}
主站蜘蛛池模板: 卢湾区| 五峰| 嘉义市| 眉山市| 仁化县| 南城县| 碌曲县| 勐海县| 丹寨县| 安陆市| 莎车县| 信丰县| 彰化县| 吴旗县| 和平区| 金堂县| 阳谷县| 孝昌县| 白沙| 广元市| 五莲县| 交城县| 阿克苏市| 富锦市| 历史| 文山县| 浦江县| 德安县| 旬邑县| 德格县| 皋兰县| 青海省| 乌拉特后旗| 揭西县| 油尖旺区| 长阳| 潢川县| 托克逊县| 旺苍县| 龙江县| 天峻县|