特殊的shell變數

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   檔案   

man bash:

  Special Parameters
       The shell treats several parameters specially.   These  parameters  may
       only be referenced; assignment to them is not allowed.
       *      Expands  to  the positional parameters, starting from one.  When
              the expansion occurs within double quotes, it expands to a  sin‐
              gle word with the value of each parameter separated by the first
              character of the IFS special variable.  That is, "$*" is equiva‐
              lent to "$1c$2c...", where c is the first character of the value
              of the IFS variable.  If IFS is unset, the parameters are  sepa‐
              rated  by  spaces.   If  IFS  is null, the parameters are joined
              without intervening separators.
       @      Expands to the positional parameters, starting from  one.   When
              the  expansion  occurs  within  double  quotes,  each  parameter
              expands to a separate word.  That is, "[email protected]" is equivalent to "$1"
              "$2"  ...   If the double-quoted expansion occurs within a word,
              the expansion of the first parameter is joined with  the  begin‐
              ning  part  of  the original word, and the expansion of the last
              parameter is joined with the last part  of  the  original  word.
              When  there  are no positional parameters, "[email protected]" and [email protected] expand to
              nothing (i.e., they are removed).
       #      Expands to the number of positional parameters in decimal.
       ?      Expands to the exit status of the most recently  executed  fore‐
              ground pipeline.
       -      Expands  to  the  current option flags as specified upon invoca‐
              tion, by the set builtin command, or  those  set  by  the  shell
              itself (such as the -i option).
       $      Expands  to  the  process ID of the shell.  In a () subshell, it
              expands to the process ID of the current  shell,  not  the  sub‐
              shell.
       !      Expands  to  the  process ID of the most recently executed back‐
              ground (asynchronous) command.
       0      Expands to the name of the shell or shell script.  This  is  set
              at shell initialization.  If bash is invoked with a file of com‐
              mands, $0 is set to the name of that file.  If bash  is  started
              with  the  -c option, then $0 is set to the first argument after
              the string to be executed, if one is present.  Otherwise, it  is
              set  to  the file name used to invoke bash, as given by argument
              zero.
       _      At shell startup, set to the absolute pathname  used  to  invoke
              the  shell or shell script being executed as passed in the envi‐
              ronment or argument list.  Subsequently,  expands  to  the  last
              argument  to the previous command, after expansion.  Also set to
              the full pathname used  to  invoke  each  command  executed  and
              placed in the environment exported to that command.  When check‐
              ing mail, this parameter holds the name of the  mail  file  cur‐
              rently being checked.

 

 

http://blog.sina.com.cn/s/blog_6739945f0100sosu.html

特殊的shell變數:

$0  擷取當前執行的shell指令碼的檔案名稱

$n  擷取當前執行的shell指令碼的第n個參數值,n=1..9

$*  擷取當前shell的所有參數 “$1 $2 $3 …,受IFS控制

$#  擷取當前shell命令列中參數的總個數

$$  擷取當前shell的進程號(PID)

$!  執行上一個指令的PID

$?  擷取執行的上一個指令的傳回值(0 為成功, 非零為失敗)

[email protected]  這個程式的所有參數 “$1″ “$2″ “$3″ “…”,不受IFS控制

註:

IFS:IFS(Internal Field Seperator)在Linux的shell中預設的分隔字元,用來把command line分解成word(欄位)。IFS可以是White Space(空白鍵)、Tab( 表格鍵)、Enter( 斷行符號鍵)中的一個或幾個。

對於[email protected]與$*的區別如下:
    這兩個參數的不同點主要在於它們擴充參數的方式不同。當使用$*時,只是簡單地在不保留引用的情況下擴充每個參數,這樣會存在問題:當參數中存在空格時,該參數將被識別為多個參數使用,如:mytar-t "my tarfile.tar",使用$*時將被識別成my、tar、file.tar坧個檔案,而使用[email protected]時則識別成一個檔案。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.