[[email protected] sh]# aa=11
[[email protected] sh]# bb=22
#給變數aa和bb賦值
[[email protected] sh]# cc=$aa+$bb
#我想讓cc的值是aa和bb的和
[[email protected] sh]# echo $cc
11+22
#但是cc的值卻是"11+22"這個字串,並沒有進行數值運算
[[email protected] ~]# declare [+/-] [選項] 變數名
選項:
[[email protected] ~]# aa=11
[[email protected] ~]# bb=22
#給變數aa和bb賦值
[[email protected] ~]# declare -i cc=$aa+$bb #宣告變數cc的型別是整數型,它的值是aa和bb的和
[[email protected] ~]# echo $cc
33
#這下終於可以相加了
[[email protected] ~]# name[0]="zhang san"
#陣列中第一個變數是張三
[[email protected] ~]# name[1]="li ming"
#陣列中第二個變數是李明
[[email protected] ~]# name[2]="gao luo feng"
#陣列中第三個變數是高洛峰
[[email protected] ~]# echo ${name}
zhang san
#輸出陣列的內容。如果只寫陣列名,那麼只會輸出第一個下標變數
[[email protected] ~]# echo ${name[*]}
zhang san li ming gao luo feng
#輸出陣列所有的內容
[[email protected] ~]# declare -x test=123
#把變數test宣告為環境變數
[[email protected] ~]# declare -r test
#給test變數賦予唯讀屬性
[[email protected] ~]#test=456
-bash:test: readonly variable
#test變數的值就不能修改了
[[email protected] ~]# declare +r test
-bash:declare:test:readonly variable
#也不能取消唯讀屬性
[[email protected] ~]# unset test
-bash: unset: test: cannot unset: readonly variable
#也不能刪除變數
[[email protected] ~]# declare -p cc
declare -i cc="33"
#cc變數是int型
[[email protected] ~]# declare -p name
declare -a name='([0]="zhang san" [1]="li ming" [2]="gao luo feng")'
#name變數是陣列型
[[email protected] ~]# declare -p test
declare -rx test="123"
#test變數是環境變數和唯讀變數
[[email protected] ~]# declare +x test
#取消test變數的環境變數屬性
[[email protected] ~]# declare -p test
declare-rtest="123"
#注意:唯讀變數屬性是不能被取消的
[[email protected] ~]# aa=11
[[email protected] ~]# bb=22
#給變數aa和bb賦值
[[email protected] ~]# dd=$(expr $aa + $bb)
#dd的值是aa和bb的和。注意"+"號左右兩側必須有空格
[[email protected] ~]# echo $dd
33
[[email protected] ~]# aa=11
[[email protected] ~]# bb=22
#給變數aa和bb賦值
[[email protected] ~]# let ee=$aa+$bb
[[email protected] ~]# echo $ee
33
#變數ee的值是aa和bb的和
[[email protected] ~]# n=20
#定義變數n
[[email protected] ~]# let n+=1
#變數n的值等於變數本身再加1
[[email protected] ~]# echo $n
21
[[email protected] ~]# aa=11
[[email protected] ~]# bb=22
[[email protected] ~]# ff=$(( $aa+$bb))
[[email protected] ~]# echo $ff
33
#變數ff的值是aa和bb的和
[[email protected] ~]# gg=$[ $aa+$bb ]
[[email protected] ~]# echo $gg
33
#變數gg的值是aa和bb的和