shell变量常用方法

时间:2021-08-02 15:34:10

变量之数组操作:

参考网址:http://www.jb51.net/article/55253.htm

#直接赋值
[root@local-yum yum.repos.d]# name[]=chengd
[root@local-yum yum.repos.d]# name[]=xrd
[root@local-yum yum.repos.d]# name[]=cxx
#小括号赋值
[root@local-yum yum.repos.d]# age=( )
#遍历数组变量
[root@local-yum yum.repos.d]# for i in ${name[@]};do echo $i ;done
chengd
xrd
cxx
[root@local-yum yum.repos.d]# for i in ${age[@]};do echo $i ;done #获取数组长度
[root@local-yum yum.repos.d]# echo ${#name[@]} [root@local-yum yum.repos.d]# echo ${#age[*]} #删除数组元素
[root@local-yum yum.repos.d]# unset name[]
[root@local-yum yum.repos.d]# echo ${#name[@]} [root@local-yum yum.repos.d]# for i in ${name[@]};do echo $i ;done
chengd
cxx
#删除数组
[root@local-yum yum.repos.d]# unset name
[root@local-yum yum.repos.d]# echo ${#name[@]} [root@local-yum yum.repos.d]# for i in ${name[@]};do echo $i ;done