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

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[*]}
主站蜘蛛池模板: 湟中县| 蓬安县| 张家川| 武平县| 利辛县| 玉山县| 怀仁县| 邯郸县| 金秀| 镇远县| 湘潭市| 沁水县| 横山县| 建宁县| 东乡族自治县| 垣曲县| 东安县| 山东| 大悟县| 老河口市| 宁安市| 织金县| 大庆市| 民县| 丽江市| 石家庄市| 泗阳县| 黎平县| 长白| 郸城县| 彭州市| 积石山| 南京市| 天全县| 温州市| 永嘉县| 台州市| 邮箱| 嘉峪关市| 康定县| 胶南市|