bash 从文件路径中删除第一个目录组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5311956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove first directory components from path of file
提问by Libor Zapletal
I need to remove one directory (the leftmost) from variables in Bash. I found ways how can I remove all the path or use dirname
and others but it was removing all or one path component on the right side; it wouldn't help me. So you have a better understanding of what I need, I'll write an example:
我需要从 Bash 中的变量中删除一个目录(最左边的)。我找到了如何删除所有路径或使用dirname
和其他路径的方法,但它删除了右侧的所有或一个路径组件;这对我没有帮助。所以你对我需要的东西有更好的了解,我会写一个例子:
I have a/project/hello.c
, a/project/docs/README
, ... and I want to remove that a/
so after some commands I′ll have project/hello.c
and project/docs/README
, ...
我有a/project/hello.c
, a/project/docs/README
, ... 我想删除它,a/
所以在一些命令之后我会有project/hello.c
和project/docs/README
, ...
回答by Jonathan Leffler
You can use any of:
您可以使用以下任何一种:
x=a/b/c/d
y=a/
echo ${x#a/}
echo ${x#$y}
echo ${x#*/}
All three echo commands produce b/c/d
; you could use the value in any way you choose, of course.
所有三个 echo 命令都会产生b/c/d
; 当然,您可以以您选择的任何方式使用该值。
The first is appropriate when you know the name you need to remove when writing the script.
当您知道在编写脚本时需要删除的名称时,第一个是合适的。
The second is appropriate when you have a variable that contains the prefix you need to remove (minor variant: y=a; echo ${x#$y/}
).
当您的变量包含需要删除的前缀(次要变量:)时,第二个是合适的y=a; echo ${x#$y/}
。
The third is the most general - it removes any arbitrary prefix up to the first slash. I was pleasantly surprised to find that the *
worked non-greedily when I tested it with bash
(version 3.2) on MacOS X 10.6.6 - I'll put that down to too much Perl and regex work (because, when I think about it, *
in shell doesn't include slashes).
第三个是最通用的 - 它删除了第一个斜杠之前的任何任意前缀。*
当我bash
在 MacOS X 10.6.6 上使用(版本 3.2)测试它时,我惊喜地发现它非贪婪地工作- 我会把它归结为太多的 Perl 和正则表达式工作(因为,当我想到它时,*
在 shell 中不包含斜杠)。
回答by Erik
echo "a/project/hello.c" | sed 's,^[^/]*/,,'
回答by Neil McGill
echo a/project/hello.c | cut -d'/' -f2-
回答by tgdavies
Look at man expr
看着 man expr
expr "foo/bar/baz" : '[^/]*/\(.*\)'
will do what you want.
expr "foo/bar/baz" : '[^/]*/\(.*\)'
会做你想做的。
回答by typelogic
None of the examples above solved my problem. I wanted to be able to switch java versionsby changing the $PATHvalue. After googling and cannot find sufficient answer, I weaved my own solution below.
上面的例子都没有解决我的问题。我希望能够通过更改$PATH值来切换 java 版本。谷歌搜索后找不到足够的答案,我在下面编织了自己的解决方案。
Here is an excerpt in my .bashrc:
这是我的.bashrc 中的一段摘录:
jv8() {
export JAVA_HOME=/opt/jdk1.8.0_121
y=$(echo $PATH | tr ':' '\n' |sed '/\/opt\/jdk/d' | tr '\n' ':')
export PATH=$JAVA_HOME/bin:$y
}
jv6() {
export JAVA_HOME=/opt/jdk1.6.0_45
y=$(echo $PATH | tr ':' '\n' |sed '/\/opt\/jdk/d' | tr '\n' ':')
export PATH=$JAVA_HOME/bin:$y
}
So, in my bash shell, I can toggle simply by:
所以,在我的 bash shell 中,我可以简单地切换:
$> jv6
java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
$> jv8
java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)