bash - 如何将which命令的结果通过管道传输到cd
如何将 which
命令的结果传送到 cd
?
这就是我想做的:
which oracle | cd
cd < which oracle
但它们都不起作用。
有没有办法实现这一点(当然不是复制/粘贴)?
编辑:再想一想,此命令会失败,因为目标文件不是文件夹/目录。
所以我正在思考并制定出一种更好的方法来摆脱尾随的“/oracle”部分(sed或awk,甚至Perl):)
编辑: 好吧,这就是我最终得到的:
cd `which oracle | sed 's/\/oracle//g'`
How could I pipe the result from a which
command to cd
?
This is what I am trying to do:
which oracle | cd
cd < which oracle
But none of them works.
Is there a way to achieve this (rather than copy/paste of course)?
Edit : on second thought, this command would fail, because the destination file is NOT a folder/directory.
So I am thinking and working out a better way to get rid of the trailing "/oracle" part now (sed or awk, or even Perl) :)
Edit :
Okay that's what I've got in the end:
cd `which oracle | sed 's/\/oracle//g'`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果命令需要来自标准输入的参数,则可以使用管道。 (更多信息)。
使用 cd 命令时情况并非如此。目录是命令参数。在这种情况下,您可以使用命令替换。使用反引号或
$(...)
来评估命令,将其存储到变量中..尽管它可以以更简单的方式完成:
或者如果您的路径有特殊字符
或
相当于反引号表示法,但建议使用(反引号可能与撇号混淆)
..但它看起来像您想要的:(
这表明您可以轻松使用嵌套)
$(...)
(以及作为反引号)也适用于双引号字符串,这在结果最终可能包含空格时有所帮助。(请注意,两个输出都需要一组双引号。)
You use pipe in cases where the command expects parameters from the standard input. ( More on this ).
With
cd
command that is not the case. The directory is the command argument. In such case, you can use command substitution. Use backticks or$(...)
to evaluate the command, store it into variable..although it can be done in a much simpler way:
or if your path has special characters
or
which is equivalent to backtick notation, but is recommended (backticks can be confused with apostrophes)
.. but it looks like you want:
(which shows you that you can use nesting easily)
$(...)
(as well as backticks) work also in double-quoted strings, which helps when the result may eventually contain spaces..(Note that both outputs require a set of double quotes.)
使用 dirname 获取目录:
编辑:注意包含空格的路径,请参阅下面的 @anishpatel 评论
With dirname to get the directory:
EDIT: beware of paths containing spaces, see @anishpatel comment below
请注意这些是反引号(通常是美式键盘上 1 左侧的键)
Note those are backticks (generally the key to the left of 1 on a US keyboard)
好的,这里有一个使用正确引用的解决方案:
避免反引号,它们的可读性较差,并且始终引用进程替换。
OK, here a solution that uses correct quoting:
Avoid backticks, they are less readable, and always quote process substitutions.
你不需要管道,你可以使用 Bash 参数扩展来做你想做的事!
进一步提示:如果您使用的是 Bash,请使用“type -P”而不是外部“which”命令。
You don't need a pipe, you can do what you want using Bash parameter expansion!
Further tip: use "type -P" instead of the external "which" command if you are using Bash.
除了上面的好答案之外,需要提到的一件事是 cd 是一个内置的 shell,它在同一个进程中运行,而不是像 ls 这样的新进程,它是一个命令。
https ://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd
http://en.wikipedia.org/wiki/Shell_builtin
besides good answer above, one thing needs to mention is that cd is a shell builtin, which run in the same process other than new process like ls which is a command.
https://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd
http://en.wikipedia.org/wiki/Shell_builtin
为了回答您编辑的问题,您可以使用
dirname
去掉命令的名称:In response to your edited question, you can strip off the name of the command using
dirname
: