110

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'`
3
  • 2
    which gives you command, not directory
    – Anycorn
    Aug 9, 2010 at 5:26
  • are you trying to get directory where program is installed? dirname, basename ?
    – Anycorn
    Aug 9, 2010 at 5:37
  • @aaa : you are right, I think dirname is better because it points to "the real dir" not a symbolic link to the oracle executable...which is why my sed apporach would fail :) Aug 9, 2010 at 6:01

7 Answers 7

157

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..

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.)

4
  • Cool. Could you please explain a bit about what is going on here? That looks like a variable dereference; is there some default temporary variable where the results of a command go?
    – Nate W.
    Aug 9, 2010 at 5:26
  • Ahhh, I forgot compelete about the backticks :( Aug 9, 2010 at 5:27
  • 4
    You need another pair of quotes: cd "$(dirname "$(which oracle)")".
    – Philipp
    Aug 9, 2010 at 11:05
  • The | opens a new process, so even if cd read from STDIN, cmd | cd wouldn't work (i.e. the current directory in the original process would remain the same). Apr 2, 2014 at 22:35
27

With dirname to get the directory:

cd $(which oracle | xargs dirname)

EDIT: beware of paths containing spaces, see @anishpatel comment below

1
  • 4
    Use cd "$(which oracle | xargs -0 dirname)" if the path may contain whitespace. The -0 flag splits input by null rather than whitespace.
    – anishpatel
    May 4, 2017 at 21:24
12
cd `which oracle`

Note those are backticks (generally the key to the left of 1 on a US keyboard)

2
  • 1
    This doesn't work if the path contains spaces or other "special" characters, and you have to strip off the file name.
    – Philipp
    Aug 9, 2010 at 11:08
  • @Philipp you can use this if your path has special characters cd "`which oracle`"
    – Ram Patra
    Jul 9, 2016 at 15:10
4

OK, here a solution that uses correct quoting:

cd "$(dirname "$(which oracle)")"

Avoid backticks, they are less readable, and always quote process substitutions.

2

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.

# 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
0
2

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

1

In response to your edited question, you can strip off the name of the command using dirname:

cd $(dirname `which oracle`)
2
  • This doesn't work if the path contains spaces or other "special" characters.
    – Philipp
    Aug 9, 2010 at 11:09
  • 1
    Easily fixed by double-quoting it.
    – David Z
    Aug 9, 2010 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.