- Learning Linux Shell Scripting
- Ganesh Naik
- 144字
- 2021-06-25 22:02:57
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[*]}
推薦閱讀
- Mastering VMware vSphere 6.5
- Photoshop CS4經典380例
- 輕松學Java
- 計算機網絡應用基礎
- 空間傳感器網絡復雜區域智能監測技術
- 21天學通ASP.NET
- OpenStack Cloud Computing Cookbook(Second Edition)
- 計算機網絡技術基礎
- ESP8266 Home Automation Projects
- HTML5 Canvas Cookbook
- Applied Data Visualization with R and ggplot2
- 奇點將至
- Mastering GitLab 12
- ZigBee無線通信技術應用開發
- Visual Basic項目開發案例精粹