当前位置:网站首页>Shell array
Shell array
2022-06-24 08:37:00 【Chen Bucheng I】
You can store multiple values in an array .Bash Shell Only one-dimensional arrays are supported ( Multidimensional arrays are not supported ), There is no need to define the array size when initializing ( And PHP similar ). Similar to most programming languages , The subscripts of array elements are 0 Start .
Create array
Shell Arrays are represented in parentheses , Element use ” Space ” Symbol split , The syntax is as follows : array_name=(value1 value2 ... valuen)
example
#!/bin/bash
my_array=(A B "C" D)
We can also use subscripts to define arrays :
array_name[0]=value0
array_name[1]=value1
array_name[2]=value2
Read array
The general format for reading array element values is : ${array_name[index]}
example
#!/bin/bash
my_array=(A B "C" D)
echo " The first element is : ${my_array[0]}"
echo " The second element is : ${my_array[1]}"
echo " The third element is : ${my_array[2]}"
echo " The fourth element is : ${my_array[3]}"
Execute the script , The output is as follows :
$ chmod +x test.sh
$ ./test.sh
The first element is : A
The second element is : B
The third element is : C
The fourth element is : D
Get all the elements in the array
Use @ or * You can get all the elements in the array
for example :
#!/bin/bash
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo " The elements of the array are : ${my_array[*]}"
echo " The elements of the array are : ${my_array[@]}"
Execute the script , The output is as follows :
$ chmod +x test.sh
$ ./test.sh
The elements of the array are : A B C D
The elements of the array are : A B C D
Gets the length of the array
The way to get the array length is the same as the way to get the string length
for example :
#!/bin/bash
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo " The number of array elements is : ${#my_array[*]}"
echo " The number of array elements is : ${#my_array[@]}"
Execute the script , The output is as follows :
$ chmod +x test.sh
$ ./test.sh
The number of array elements is :
4
The number of array elements is :
4
边栏推荐
- Live broadcast appointment: growth of Mengxin Product Manager
- QTimer定时器不起作用的原因
- Easydss anonymous live channel data volume instability optimization scheme sharing
- 权限模型 DAC ACL RBAC ABAC
- 2022 mobile crane driver special operation certificate examination question bank and online simulation examination
- 貸款五級分類
- io模型初探
- 新技术实战,一步步用Activity Results API封装权限申请库
- 05 Ubuntu installing mysql8
- Common CVM transcribes audio using virtual sound card
猜你喜欢
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
io模型初探
Question 3 - MessageBox pop-up box, modify the default background color
分布式 | 如何与 DBLE 进行“秘密通话”
Qt导出PDF文件的两种方法
WCF TCP protocol transmission
Longhorn installation and use
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
【微服务~Nacos】Nacos服务提供者和服务消费者
Synthesize video through ffmpeg according to m3u8 file of video on the network
随机推荐
The reason why the qtimer timer does not work
"Wechat cloud hosting" first practical battle | introduction to minimalist demo
Question 3 - MessageBox pop-up box, modify the default background color
Introduction to RCNN, fast RCNN and fast RCNN
Rust procedure macro simply imitates Lombok function
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
Promise usage scenarios
中国芯片独角兽公司
QT writing security video monitoring system 36 onvif continuous movement
Redis的Cluster集群数据倾斜
ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
After interviewing and tutoring several children, I found some problems!
AUTO PWN
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
Nodejs redlock notes
Longhorn installation and use
487. 最大连续1的个数 II ●●
Introduction to NC machine tool programming [G-code]
Review SGI STL secondary space configurator (internal storage pool) | notes for personal use