当前位置:网站首页>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/bashmy_array=(A B "C" D)We can also use subscripts to define arrays :array_name[0]=value0array_name[1]=value1array_name[2]=value2
Read array
The general format for reading array element values is : ${array_name[index]}
example#!/bin/bashmy_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.shThe first element is : AThe second element is : BThe third element is : CThe 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/bashmy_array[0]=Amy_array[1]=Bmy_array[2]=Cmy_array[3]=Decho " 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.shThe elements of the array are : A B C DThe 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/bashmy_array[0]=Amy_array[1]=Bmy_array[2]=Cmy_array[3]=Decho " 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.shThe number of array elements is :4The number of array elements is :4
边栏推荐
- 小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
- Fund raising, trading and registration
- 小黑ai4code代码baseline啃食1
- ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
- 11-- longest substring without repeated characters
- PAT 1157:校庆
- Micro build low code online "quick registration applet" capability
- Ordinary token
- win11在cmder中使用vim查看内容的时候空白
- Chart list Performance Optimization: minimum resource consumption in the visualization area
猜你喜欢
随机推荐
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
List of Li Bai's 20 most classic poems
Tencent conference API - get rest API & webhook application docking information
How to configure networkpolicy for nodeport in kubernetes
JS to get the last element of the array
ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
2022 mobile crane driver special operation certificate examination question bank and online simulation examination
ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
一文带你了解Windows操作系统安全,保护自己的电脑不受侵害
ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
Detailed explanation of etcd backup and recovery principle and actual record of stepping on the pit
Maya re deployment
Paper notes: multi label learning dm2l
RCNN、Fast-RCNN、Faster-RCNN介绍
How to implement approval function in Tekton
Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing
After interviewing and tutoring several children, I found some problems!
Easydss anonymous live channel data volume instability optimization scheme sharing
487. number of maximum consecutive 1 II ●●
Cloudbase database migration scheme









