簡體   English   中英

Shell腳本參數替換

[英]Shell script parameter substitution

有人能告訴我這兩個約定之間有什么區別來獲取當前正在運行的腳本的名稱?

#!/bin/sh

echo $0
echo ${0##*/}

第二個版本是否從腳本名稱中刪除路徑?

謝謝!

你的猜測是正確的。 從我的機器上的bash文檔:

${parameter#word}
   ${parameter##word}
          The  word  is  expanded to produce a pattern just as in pathname
          expansion.  If the pattern matches the beginning of the value of
          parameter,  then  the  result  of  the expansion is the expanded
          value of parameter with the shortest matching pattern (the ``#''
          case) or the longest matching pattern (the ``##'' case) deleted.
          If parameter is @ or *, the pattern removal operation is applied
          to  each  positional parameter in turn, and the expansion is the
          resultant list.  If parameter is an array  variable  subscripted
          with  @  or  *, the pattern removal operation is applied to each
          member of the array in turn, and the expansion is the  resultant
          list.

是的,第二個版本使用bash擴展來剪切與* / regexp匹配的$ 0的最長前綴,所以如果$ 0是“/ bin / bash”,那么$ {0 ## * /}將是“bash”。 您可以使用`basename $ 0`來獲得相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM