3.5-变量数组

环境变量可以当做数组使用。

1、变量数组概念
 数组是能保存多个值的变量,数组中的值可以分别引用,也可以整体引用。

2、变量数组赋值
 要为某个环境变量设置多个值,只需将他们列出在圆括号中,各值以空格分隔:

[root@hadoop ~]# test_var=(one two three four five)

3、变量数组引用
 1> 引用变量数组单个元素
 使用数组索引值引用数组中的单个元素,索引表示该元素在数组中的位置,索引值包含在方括号中,索引值以0开始:

[root@hadoop ~]# echo ${test_var[0]}
one
[root@hadoop ~]# echo ${test_var[1]}
two
[root@hadoop ~]# echo ${test_var[3]}
four

 2> 引用变量数组整体
 要引用整个数组变量,使用星号通配符作为索引值:

[root@hadoop ~]# echo ${test_var[*]}
one two three four five

4、变量数组修改
 可以直接赋值给变量数组中某个值来更改某个索引位置的值:

[root@hadoop ~]# test_var[1]=two_new
[root@hadoop ~]# echo ${test_var[*]}
one two_new three four five

5、变量数组单个值移除
 1> 使用unset命令移除变量数组中的某个值
移除的是变量数组的某个值,数组的整体长度不变:

[root@hadoop ~]# echo ${test_var[*]}
one two_new three four five
[root@hadoop ~]# unset test_var[1]
[root@hadoop ~]# echo ${test_var[*]}
one three four five
[root@hadoop ~]# echo ${test_var[0]}
one
[root@hadoop ~]# echo ${test_var[1]}

[root@hadoop ~]# echo ${test_var[2]}
three
[root@hadoop ~]# echo ${test_var[4]}
five

在这里插入图片描述
 2> 使用unset命令移除整个数组

[root@hadoop ~]# test_var=(one two three four five)
[root@hadoop ~]# unset test_var
[root@hadoop ~]# echo ${test_var[*]}

[root@hadoop ~]# echo ${test_var[0]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章