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

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[*]}
主站蜘蛛池模板: 岳西县| 巴青县| 莱芜市| 陆河县| 汪清县| 万山特区| 东莞市| 葫芦岛市| 岳阳县| 长治县| 通许县| 河池市| 谢通门县| 门源| 阿城市| 凤阳县| 凭祥市| 金塔县| 裕民县| 宜春市| 清流县| 瑞安市| 太谷县| 靖州| 靖州| 城市| 富源县| 阳信县| 迁西县| 武定县| 烟台市| 鄂尔多斯市| 宁南县| 浦江县| 石渠县| 临安市| 乌鲁木齐县| 静宁县| 南靖县| 石河子市| 枝江市|