网络安全工程师成长路径:从基础到实战
2026/11/3 5:14:01
Shell中数组对元素个数没数组有限制,但只支持一维数组。Shell变量是弱类型性的,不要求每个元素都是相同类型。
1、 定义的同时赋值[root@tbds-10-1-0-38lib]# declare-a Arr6=('1''john')[root@tbds-10-1-0-38lib]# echo ${Arr6[1]}john2、 不使用关键字[root@tbds-10-1-0-38lib]# Arr7=(1"ni"2)[root@tbds-10-1-0-38lib]# echo ${Arr7[0]}1[root@tbds-10-1-0-38lib]# echo ${Arr7[1]}ni3、 跳号赋值[root@tbds-10-1-0-38lib]# Score=([2]=98[5]=79[6]=88)[root@tbds-10-1-0-38lib]# echo ${Score[2]}98取单个值[root@tbds-10-1-0-38lib]# echo ${Score[2]}98取所有的值[root@tbds-10-1-0-38lib]# echo ${Score[*]}987988数组的元素个数[root@tbds-10-1-0-38lib]# echo ${#Score[*]}3某个元素的长度[root@tbds-10-1-0-38lib]# echo ${#Score[2]}2可以是某个元素的一部分,也可以截取整个数组的某几个元素
[root@tbds-10-1-0-38lib]# arrDemo=(1"nihao""hello""how are you")[root@tbds-10-1-0-38lib]# echo ${arrDemo[1]:0:3}nih[root@tbds-10-1-0-38lib]# echo ${arrDemo[*]:0:3}1nihao hello[root@tbds-10-1-0-38lib]# Conn=(${arrDemo[@]}${Arr7[@]})[root@tbds-10-1-0-38lib]# echo ${Conn[*]}1nihao hello how are you1ni2[root@tbds-10-1-0-38lib]# arrDemo=(${arrDemo[@]/hello/HELLO})[root@tbds-10-1-0-38lib]# echo ${arrDemo[@]}1nihao HELLO how are you[root@tbds-10-1-0-38lib]# unset arrDemo[1][root@tbds-10-1-0-38lib]# echo ${arrDemo[@]}1HELLO how are you[root@tbds-10-1-0-38lib]# unset arrDemo[root@tbds-10-1-0-38lib]# echo ${arrDemo[@]}