I want to cd to the directory of go's bin file:

$ type go  
go is /c/tools/go/bin/go

How can I quickly cd to this directory in bash ?

  • What do you mean by quickly? Like, a short command that brings you to the directory? – SpecialBomb Jan 11 '16 at 14:41
  • this is dupplicate, but @Jijin's answer looks better. – Archemar Jan 11 '16 at 14:53
  • @SpecialBomb yes. this is what i expect. since I don't much about bash, so the answer is open, maybe there some keybord shortcut to let me do this kind of things. – lovespring Jan 11 '16 at 15:06
 cd $(dirname $(which go))

which go will show the path of the executable. Then get the dirname of that path and cd to that.

  • won't which return the path to the executable file? cd will never work if it is true – lese Jan 11 '16 at 14:49
  • @lese dirname removes the file at the end by checking if it's a file, so it works. – SpecialBomb Jan 11 '16 at 14:51
  • What if there is no executable, shouldn't you handle the error... – 123 Jan 11 '16 at 15:02
  • 1
    which go && cd $(dirname $(which go)) . Won't cd if first which go fails. – 7171u Jan 11 '16 at 15:16

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