linux中預留的$變數

來源:互聯網
上載者:User

標籤: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中預留的$變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.