bash - how to pipe result from the which command t

2019-03-07 18:08发布

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'`

7条回答
Fickle 薄情
2楼-- · 2019-03-07 18:55

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.

  1. https://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd

  2. http://en.wikipedia.org/wiki/Shell_builtin

查看更多
登录 后发表回答