代码之家  ›  专栏  ›  技术社区  ›  user3313834

如何使用bash从除第一个文件夹之外的所有路径获取

  •  0
  • user3313834  · 技术社区  · 6 年前

    如何使用bash从除第一个文件夹之外的所有路径获取?

    例如

    'a/b/d/e' 得到 'b/d/e'

    现在我用蟒蛇做这个

    $ pip install pythonpy
    $ echo 'a/b/d/e' | py -x '"/".join(x.split("/")[1:])'
    b/d/e
    $
    

    Cyrius解决方案工作良好:

    $ x='a/b/d/e'
    $ echo "${x#*/}"
    b/d/e
    $
    

    但看完后 Parameter Expansion 唯一相关的例子 ${parameter#...} 是::

    ${parameter#word}
    ${parameter##word}
    
      The word is expanded to produce a pattern just as in filename expansion (see 
      Filename Expansion). If the pattern matches the beginning of the expanded 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.
    

    这对我理解什么是由 "${x#*/}"

    1 回复  |  直到 6 年前
        1
  •  2
  •   Cyrus    6 年前

    用bash及其 Parameter Expansion 以下内容:

    x='a/b/d/e'
    echo "${x#*/}"
    

    输出:

    b/d/e