標籤:style blog http color ar 使用 for sp 檔案
$0表示bash指令碼的檔案名稱$1表示第一個參數$*表示參數列表$0, $1, $2…[email protected]表示"$1"/"$2"...每個變數都是獨立的,用雙引號括起來$#表示參數列表個數$?表示上一條命令的執行結果$!表示上一條命令的PID號 注意:不被引用的$*每個單詞被當成獨立的單詞使用"$*"整個參數列表被當成一個參數不引用的#@與$*一樣"[email protected]"保留調用者的假設,引號內的作為一個參數,且保留空格 下面的指令碼代碼來對$*和[email protected]來進行相應的測試
1 #!/bin/bash 2 3 # The differences between $* [email protected] "$*" "[email protected]" 4 5 echo Dollar star is $* # 空格未保留 6 echo "Dollar star in double quotes is $*" # 引號內的空格被保留 7 echo Dollar at is [email protected] # 空格未保留 8 echo "Dollar at in double quotes is [email protected]" #引號的空格被保留 9 10 echo11 echo "Looping through Dollar Star" # 不引用$* 每個單詞被當成獨立的單詞12 for i in $*13 do14 echo "Parameter is $i"15 done16 17 echo18 echo "Looping through Dollar Star with double quotes" # 使用"$*"整個參數列表被當成一個參數19 for i in "$*"20 do21 echo "Parameter is $i"22 done23 24 echo25 echo "Looping through Dollar At" # 不引用的#@與$*一樣26 for i in [email protected]27 do28 echo "Parameter is $i"29 done30 31 echo32 echo "Looping through Dollar At in double quotes" # "[email protected]"保留調用者的假設,引號內的作為一個參數,且保留空格33 for i in "[email protected]"34 do35 echo "Parameter is $i"36 done
運行命令:
bash 01defaultParam.sh "hello world" two
執行結果:
Dollar star is hello world twoDollar star in double quotes is hello world twoDollar at is hello world twoDollar at in double quotes is hello world two Looping through Dollar StarParameter is helloParameter is worldParameter is two Looping through Dollar Star with double quotesParameter is hello world two Looping through Dollar AtParameter is helloParameter is worldParameter is two Looping through Dollar At in double quotesParameter is hello worldParameter is two
對應新浪部落格地址:http://blog.sina.com.cn/s/blog_6941438f0102v8qj.html
linux中預留的$變數