本文介绍了bash-最后一个特定字符后的表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foo="/sdf/here/jfds"
bar="${foo##*/}"

Canyone解释了"${foo##*/}"表达式的工作原理,因为我知道它会在最后一个正斜杠(即jfds)之后返回字符串,但我不知道它是如何执行的(或称为这种类型的表达式) ?

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)?

推荐答案

它是几种shell功能之一,通常称为 shell扩展.这种特定的扩展被称为 parameter 扩展*.

It is one of several shell features, generically called shell expansion. This particular expansion is called parameter expansion*.

您可以将这种特殊的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)..

当仅使用一个#时,表示仅左截断随后出现的模式的 first (直到结束}.当使用两个##时,表示左截断所有连续模式匹配.var="a/b/c"; echo ${var#*/}的结果为b/c ... echo ${var##*/}返回c.

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.

有一个补充的右截.它使用%而不是# ...(我记住",这是因为#就像bash注释;始终在左侧).

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.

这是所有shell扩展的列表,按优先级顺序显示.

Here is a list of all shell expansions, presented in precedence order.

扩展顺序为:

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  ]]

这篇关于bash-最后一个特定字符后的表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:00