12
foo="/sdf/here/jfds"
bar="${foo##*/}"
Canyone解釋「${foo##*/}
」表達是如何工作的,因爲我知道最後斜線後,將返回字符串(即jfds),但我不知道它是怎麼做的(或這種類型的表達被稱爲)?慶典 - 最後一個特定字符後表達
foo="/sdf/here/jfds"
bar="${foo##*/}"
Canyone解釋「${foo##*/}
」表達是如何工作的,因爲我知道最後斜線後,將返回字符串(即jfds),但我不知道它是怎麼做的(或這種類型的表達被稱爲)?慶典 - 最後一個特定字符後表達
它是幾個外殼的特徵之一,總稱爲殼膨脹。這個特殊的擴展被稱爲參數擴展*。
您可以將此特定的shell擴展形式看作爲左截斷字符串函數。如果您只使用一個#
必須使用大括號如圖所示(即不可選)..
,這意味着左截斷僅如下(截至收盤}
模式的第一發生。當您使用兩個##
,這意味着左截斷所有連續的圖案匹配。的var="a/b/c"; echo ${var#*/}
結果是b/c
... echo ${var##*/}
回報c
。
有一個互補右截斷。它使用%
代替Ø f #
...(我「記得」哪個是因爲#
就像bash的評論;總是在左邊)。
*
被視爲bash通配符擴展。
以下是按優先順序顯示的所有shell擴展的列表。
擴展的順序是:
1. brace expansion ... prefix{-,\,}postfix # prefix-postfix prefix,postfix
.. {oct,hex,dec,bin} # oct hex dec bin
. {a..b}{1..2} # a1 a2 b1 b2
. {1..04} # 01 02 03 04
. {01..4} # 01 02 03 04
. {1..9..2} # 1 3 5 7 9
. \$\'\\x{0..7}{{0..9},{A..F}}\' # $'\x00' .. $'\x7F'
2. tilde expansion .... ~ # $HOME
... ~axiom # $(dirname "$HOME")/axiom
... ~fred # $(dirname "$HOME")/fred
.. ~+ # $PWD (current working directory)
.. ~- # $OLDPWD (previous working directory. If OLDPWD is unset,
~- is not expanded. ie. It stays as-is,
regardless of the state of nullglob.)
# Expansion for Directories in Stack. ie.
# The list printed by 'dirs' when invoked without options
. ~+N # Nth directory in 'dirs' list (from LHS)
. ~-N # Nth directory in 'dirs' list (from RHS)
3. parameter expansion .... ${VAR/b/-dd-}
... ${TEST_MODE:-0}
.. ${str: -3:2} # note space after :
. ${#string}
4. (processed left-to-right)
variable expansion
arithmetic expansion
command substitution
▶5. word splitting # based on $IFS (Internal Field Seperator)
▷6. pathname expansion
according to options such as:
nullglob, GLOBIGNORE, ...and more
# Note: ===============
▶ 5. word splitting ↰
▷ 6. pathname expansion ↰
# ===================== ↳ are not performed on words between [[ and ]]
這就是所謂的[參數擴展](http://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion)。該鏈接還解釋了「它是如何工作的」,即描述它的功能。或者你有更具體的問題嗎? – 2012-03-02 11:45:20