views:

122

answers:

6

Hi all:

I know this must be a stupid question, but 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)?

Thanks a lot in advance!

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'`
+8  A: 

you would use pipe in case if cd expects the directory in the standard input, which 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 much simpler way:

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))"
mykhal
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?
Shakedown
@mykhal : Ahhh, I forgot compelete about the backticks :(
Michael Mao
You need another pair of quotes: `cd "$(dirname "$(which oracle)")"`.
Philipp
+2  A: 
cd `which oracle`

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

Cfreak
This doesn't work if the path contains spaces or other "special" characters, and you have to strip off the file name.
Philipp
+1  A: 

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

cd $(dirname `which oracle`)
David Zaslavsky
This doesn't work if the path contains spaces or other "special" characters.
Philipp
Easily fixed by double-quoting it.
David Zaslavsky
+1  A: 

With dirname to get the directory:

cd $(which oracle | xargs dirname)
RC
+2  A: 

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
bashfu
+1  A: 

OK, here a solution that uses correct quoting:

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

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

Philipp