Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I would like to do something similar to the following:

which someapplciation | cd outputfrompreviouscommand

The command which provides a directory and I would like to be able to make that output my current working directory without using a programming language i.e. awk, bash, perl, etc. and to only use the pipe command.

To further give an example:

which vi

provides the output


I would like my working directory to be moved to that directory which I can test by using the pwd command which should have the output that matches of /some/dir.

share|improve this question

marked as duplicate by Gilles linux Jun 10 '14 at 23:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Actually, which executable provides the output /path/to/executable, not its directory. So, cding to the output of which won't work. – HalosGhost Jun 10 '14 at 22:57
up vote 4 down vote accepted

You can't use a pipe if the second command you are running doesn't read from its standard input. However, you can do something like

cd $(which someapplication)

or, since you need a directory name for cd and not an executable name:

cd $(dirname $(which someapplication))

The $(...) shell operator executes the command within parentheses and substitutes its output into the command line.

share|improve this answer

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