Bash:在目录 `directory_name.git` 中,如何 cd 到 `../directory_name`?
我对 Bash 脚本相当陌生,希望执行以下操作:
脚本的 pwd 是“/a/b/c/directory_name.git/”,我想 cd 到“../directory_name”,其中directory_name 可以可以是任何东西。有什么简单的方法可以做到这一点吗?
我猜我必须将 pwd
的结果放入变量中并删除最后 4 个字符。
I'm pretty new to Bash scripting and am looking to do the following:
The script's pwd is "/a/b/c/directory_name.git/" and I'd like to cd to "../directory_name" where directory_name could be anything. Is there any easy way to do this?
I'm guessing I'd have to put the result of pwd
in a variable and erase the last 4 characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或更简单
测试
*注意:$PWD 不包含尾部斜杠,因此
${param##word}
扩展可以正常工作。or perhaps more simply
Test
*Note: $PWD does not include a trailing slash so the
${param##word}
expansion will work just fine.尝试:
反引号执行里面的命令,并使用那个命令的输出作为
cd
的命令行参数。要调试这样的管道,使用
echo
很有用:Try:
The backticks execute the command inside, and use the output of that command as a command line argument to
cd
.To debug pipelines like this, it's useful to use
echo
:这应该有效:
This should work:
没想到这么快就能得到这么多答案,所以我有时间想出我自己的不优雅的解决方案:
上面可能有一个更优雅的:)
Didn't expect to get so many answers so fast so I had time to come up with my own inelegant solution:
There's probably one above that's much more elegant :)
这就是
basename
的用途:That's what
basename
is for: