環境變量可以當做數組使用。
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]}