指定變數的類型: 使用declare或者typeset declare/typeset選項-r 唯讀declare -r var1(declare -r var1與readonly var1是完全一樣的)這和C語言中的const關鍵字一樣, 都用來指定變數為唯讀. 如果你嘗試修改一個唯讀變數的值, 那麼會產生錯誤資訊.root@ubuntu:~/resource/shell-study/0508-2013# declare -r var1=11 root@ubuntu:~/resource/shell-study/0508-2013# echo $var1 11 root@ubuntu:~/resource/shell-study/0508-2013# let "var1=12" bash: var1: readonly variable root@ubuntu:~/resource/shell-study/0508-2013# ^C root@ubuntu:~/resource/shell-study/0508-2013# readonly var2=12 root@ubuntu:~/resource/shell-study/0508-2013# echo $var2 12 root@ubuntu:~/resource/shell-study/0508-2013# let "var2=13" bash: var2: readonly variable root@ubuntu:~/resource/shell-study/0508-2013# -i 整型declare -i number 指令碼將會把變數"number"按照整型進行處理. 如果把一個變數指定為整型的話, 那麼即使沒有expr或者let命令, 也允許使用特定的算術運算.#!/bin/bash declare -i number number=3 echo "number = $number" number="123" echo "number = $number" number="abc" echo "number = $number" number=6/3 echo "number = $number" not_int_number=6/3 echo "not_int_number = $not_int_number" exit 0 看看結果:root@ubuntu:~/resource/shell-study/0508-2013# ./test1.sh number = 3 number = 123 number = 0 number = 2 not_int_number = 6/3 root@ubuntu:~/resource/shell-study/0508-2013# -a 數組declare -a indices 變數indices將被視為數組.root@ubuntu:~/resource/shell-study/0508-2013# declare -a str root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5) root@ubuntu:~/resource/shell-study/0508-2013# echo $str 1 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[0]} 1 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[1]} 2 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[2]} 3 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[3]} 4 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[4]} 5 root@ubuntu:~/resource/shell-study/0508-2013# 用${數組名[下標]} 下標是從0開始 下標是:*或者@ 得到整個數組內容,直接通過 數組名[下標] 就可以對其進行引用賦值,如果下標不存在,自動添加新一個數組元素,直接通過:unset 數組[下標] 可以清除相應的元素,不帶下標,清除整個資料root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5) root@ubuntu:~/resource/shell-study/0508-2013# echo $str 1 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[*]} 1 2 3 4 5 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]} 1 2 3 4 5 root@ubuntu:~/resource/shell-study/0508-2013# str[1]=10 root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]} 1 10 3 4 5 root@ubuntu:~/resource/shell-study/0508-2013# unset str root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]} root@ubuntu:~/resource/shell-study/0508-2013# -f 函數declare -f如果在指令碼中使用declare -f, 而不加任何參數的話, 那麼將會列出這個指令碼之前定義的所有函數.declare -f function_name如果在指令碼中使用declare -f function_name這種形式的話, 將只會列出這個函數的名字.root@ubuntu:~/resource/shell-study/0508-2013# declare -f command_not_found_handle () { if [ -x /usr/lib/command-not-found ]; then /usr/bin/python /usr/lib/command-not-found -- $1; return $?; else if [ -x /usr/share/command-not-found ]; then /usr/bin/python /usr/share/command-not-found -- $1; return $?; else return 127; fi; fi } root@ubuntu:~/resource/shell-study/0508-2013# declare -f hello -x exportdeclare -x var3這句將會聲明一個變數, 並作為這個指令碼的環境變數被匯出.-x var=$valuedeclare -x var3=373declare命令允許在聲明變數類型的同時給變數賦值.