Linux基本命令學習之五:bash 變數

趙明威發表於2014-09-13

一、Linux基本命令學習之 -bash 變數

變數鍵盤讀取/陣列與宣告:read array declare

read 讀取來自鍵盤輸入的變數:

read -p 提示符 -t 時間s 變數

[hadoop@hadoop scripts]$ read -p "輸入姓名:" -t 30 yourname
輸入姓名:zhaomingwei
[hadoop@hadoop scripts]$ echo $yourname
zhaomingwei
[hadoop@hadoop scripts]$ read -p "輸入姓名:" -t 30 yourname
輸入姓名:
[hadoop@hadoop scripts]$ 

不宣告型別預設為文字型別

declare 宣告變數的型別

-i 為integer型別

-a 定義陣列

-x 設定為環境變數

-r 設定為readonly

echo 用來顯示文字或者字串 "$" 用來取得變數的值

練習如下:

不宣告時結果:

[hadoop@hadoop scripts]$ sum=100+300+50
[hadoop@hadoop scripts]$ echo $sum
100+300+50

宣告時候結果:

[hadoop@hadoop scripts]$ declare -i sum=100+300+50
[hadoop@hadoop scripts]$ echo $sum
450
[hadoop@hadoop scripts]$ 
[hadoop@hadoop scripts]$ declare -i sum=100+300+50
[hadoop@hadoop scripts]$ echo $sum
450
[hadoop@hadoop scripts]$ declare -p sum
declare -i sum="450"
[hadoop@hadoop scripts]$ declare -x sum
[hadoop@hadoop scripts]$ declare -p sum
declare -ix sum="450"
[hadoop@hadoop scripts]$ declare -x sum
[hadoop@hadoop scripts]$ declare -p sum
declare -ix sum="450"
[hadoop@hadoop scripts]$ declare +x sum
[hadoop@hadoop scripts]$ declare -p sum
declare -i sum="450"

宣告陣列:

[hadoop@hadoop scripts]$ declare -a var
[hadoop@hadoop scripts]$ var[1]="small min"
[hadoop@hadoop scripts]$ var[2]="big min"
[hadoop@hadoop scripts]$ var[3]="nice min" 
[hadoop@hadoop scripts]$ echo "${var[1]},${var[2]},${var[3]}"
small min,big min,nice min

相關文章