1

I want use a command such as this :

 which node && cd $_

to jump in the directory when the binary file reside, but the which command return all the path + name of binary ,so i get an error :

 bash: cd: node: No such file or directory

I'm looking for new command move me in the right place.

cuonglm
  • 153,898

3 Answers3

3

Don't use which (unless you're in csh or tcsh variants), it's broken. Using command -v node instead.

POSIX offer dirname command to get the directory portion of pathname:

cd "$(dirname -- "$(command -v node)")"

or using a variable to store the pathname, prevent you from calling dirname:

nodepath=$(command -v node)
cd "${nodepath%/*}"
cuonglm
  • 153,898
1

Meanwhile, in ZShell, this requires the quite ugly and awkward construct:

cd -- $commands[node]:h

This even works when there's directories with spaces in their name (unless for unfathomable reasons you enabled the shwordsplit option).

thrig
  • 34,938
-1
cd $(dirname $(which bash))

Good luck :)

Archemar
  • 31,554