bash - 如何将 which 命令的结果通过管道传输到 cd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3437514/
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
bash - how to pipe result from the which command to cd
提问by Michael Mao
How could I pipe the result from a which
command to cd
?
如何将which
命令的结果通过管道传输到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) :)
所以我现在正在思考并想出一种更好的方法来摆脱尾随的“/oracle”部分(sed 或 awk,甚至 Perl):)
Edit : Okay that's what I've got in the end:
编辑:好的,这就是我最后得到的:
cd `which oracle | sed 's/\/oracle//g'`
回答by mykhal
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..
有了cd
命令,情况就不是这样了。该目录是命令参数。在这种情况下,您可以使用命令替换。使用反引号或$(...)
评估命令,将其存储到变量中..
path=`which oracle`
echo $path # just for debug
cd $path
although it can be done in a much simpler way:
虽然它可以以更简单的方式完成:
cd `which oracle`
or if your path has special characters
或者如果您的路径有特殊字符
cd "`which oracle`"
or
或者
cd $(which oracle)
which is equivalent to backtick notation, but is recommended (backticks can be confused with apostrophes)
这相当于反引号,但建议使用(反引号可能与撇号混淆)
.. but it looks like you want:
.. 但看起来你想要:
cd $(dirname $(which oracle))
(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..
$(...)
(以及反引号)也适用于双引号字符串,这有助于结果最终可能包含空格。
cd "$(dirname "$(which oracle)")"
(Note that both outputs require a set of double quotes.)
(请注意,两个输出都需要一组双引号。)
回答by Michael Mao
With dirname to get the directory:
使用 dirname 获取目录:
cd $(which oracle | xargs dirname)
EDIT: beware of paths containing spaces, see @anishpatel comment below
编辑:注意包含空格的路径,请参阅下面的@anishpatel 评论
回答by Cfreak
cd `which oracle`
Note those are backticks (generally the key to the left of 1 on a US keyboard)
注意那些是反引号(通常是美式键盘上 1 左边的键)
回答by Philipp
OK, here a solution that uses correct quoting:
好的,这里有一个使用正确引用的解决方案:
cd "$(dirname "$(which oracle)")"
Avoid backticks, they are less readable, and always quote process substitutions.
避免反引号,它们可读性较差,并且总是引用进程替换。
回答by bashfu
You don't need a pipe, you can do what you want using Bash parameter expansion!
你不需要管道,你可以使用 Bash 参数扩展做你想做的事!
Further tip: use "type -P" instead of the external "which" command if you are using Bash.
进一步提示:如果您使用 Bash,请使用“type -P”而不是外部“which”命令。
# test
touch /ls
chmod +x /ls
cmd='ls'
PATH=/:$PATH
if cmdpath="$(type -P "$cmd")" && cmdpath="${cmdpath%/*}" ; then
cd "${cmdpath:-/}" || { echo "Could not cd to: ${cmdpath:-/}"; exit 1; }
else
echo "No such program in PATH search directories: ${cmd}"
exit 1
fi
回答by David Z
In response to your edited question, you can strip off the name of the command using dirname
:
针对您编辑的问题,您可以使用dirname
以下命令去除命令名称:
cd $(dirname `which oracle`)
回答by Jiacai Liu
besides good answer above, one thing needs to mention is that cdis a shell builtin, which run in the same process other than new process like lswhich is a command.
除了上面的好答案之外,还有一件事需要提及的是cd是一个内置的 shell,它运行在同一个进程中,而不是像ls这样的新进程,它是一个命令。