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

时间:2018-12-16 11:43:24

标签: bash

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

例如

'a/b/d/e'获得'b/d/e'

现在我用pythonpy做到这一点

$ 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 个答案:

答案 0 :(得分:2)

使用bash及其Parameter Expansion

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

输出:

b/d/e