2

This is a command-line trick from a book-"The Productive Programmer":

pushd `which java`/..

The author says this command can temporarily go the directory where the executable command java lives.

When I typed this command in Linux, Bash complained that:

bash: pushd: /usr/bin/java/..: Not a directory

I also tried to change directory to /usr/bin/java/.. directly:

$ pushd /usr/bin/java/.. 
bash: pushd: /usr/bin/java/..: Not a directory

Why did I fail? Why cannot change to a directory using /..? How to modify this command to fulfill the same task?

Feng Yu
  • 175

1 Answers1

5

That fails because /usr/bin/java is a file, not a directory.

You can do:

cd "$(dirname "$(which java)")"

Or use parameter expansion in zsh:

cd "${"$(which java)"%/*}"
heemayl
  • 56,300