foo="/sdf/here/jfds"
bar="${foo##*/}"
Canyone explain how the "${foo##*/}
" expression works because I understand it will return the string after the last forward slash (ie jfds) but I have no idea how it does it (or what this type of expression is called)?
Canyone解释了“$ {foo ## * /}”表达式是如何工作的,因为我知道它将在最后一个正斜杠(即jfds)之后返回字符串,但我不知道它是如何做到的(或者这种类型的表达式是什么所谓的)?
1 个解决方案
#1
24
It is one of several shell features, generically called shell expansion. This particular expansion is called parameter expansion*.
它是几种shell特性之一,通常称为shell扩展。这种特殊的扩展称为参数扩展*。
You can think of this particular shell expansion form as a left-truncate string function. You must use the curly braces as shown (that is not optional)..
您可以将此特定shell扩展形式视为左截断字符串函数。你必须使用如图所示的花括号(这不是可选的)..
When you use only one #
, it means left-truncate only the first occurrence of the pattern which follows (up to the closing }
. When you use two ##
, it means left-truncate all consecutive pattern-matches. The result of var="a/b/c"; echo ${var#*/}
is b/c
... echo ${var##*/}
returns c
.
当你只使用一个#时,它意味着只截断第一次出现的模式(直到结束}。当你使用两个##时,它意味着左截断所有连续的模式匹配。 =“a / b / c”; echo $ {var#* /}是b / c ... echo $ {var ## * /}返回c。
There is a complementary right-truncate. It uses %
instead of the #
... (I "remember" which is which because #
is like a bash comment; always on the left).
有一个互补的右截断。它使用%而不是#...(我“记住”这是因为#就像一个bash注释;总是在左边)。
The *
is treated as a bash wildcard expansion.
*被视为bash通配符扩展。
Here is a list of all shell expansions, presented in precedence order.
以下是按优先顺序显示的所有shell扩展的列表。
The order of expansions is:
扩展的顺序是:
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 ]]
#1
24
It is one of several shell features, generically called shell expansion. This particular expansion is called parameter expansion*.
它是几种shell特性之一,通常称为shell扩展。这种特殊的扩展称为参数扩展*。
You can think of this particular shell expansion form as a left-truncate string function. You must use the curly braces as shown (that is not optional)..
您可以将此特定shell扩展形式视为左截断字符串函数。你必须使用如图所示的花括号(这不是可选的)..
When you use only one #
, it means left-truncate only the first occurrence of the pattern which follows (up to the closing }
. When you use two ##
, it means left-truncate all consecutive pattern-matches. The result of var="a/b/c"; echo ${var#*/}
is b/c
... echo ${var##*/}
returns c
.
当你只使用一个#时,它意味着只截断第一次出现的模式(直到结束}。当你使用两个##时,它意味着左截断所有连续的模式匹配。 =“a / b / c”; echo $ {var#* /}是b / c ... echo $ {var ## * /}返回c。
There is a complementary right-truncate. It uses %
instead of the #
... (I "remember" which is which because #
is like a bash comment; always on the left).
有一个互补的右截断。它使用%而不是#...(我“记住”这是因为#就像一个bash注释;总是在左边)。
The *
is treated as a bash wildcard expansion.
*被视为bash通配符扩展。
Here is a list of all shell expansions, presented in precedence order.
以下是按优先顺序显示的所有shell扩展的列表。
The order of expansions is:
扩展的顺序是:
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 ]]