0

I'm working on OSX 10.11.4 trying to write a simple bash script. I'm getting command not found when I try to use chdir. When I test with which chdir I get nothing. What's going on?

I'm using this clunky workaround...

alias vagrants_up='here=$(pwd) && cd ~/Vagrant && vagrant up && cd $(echo $here)'
emersonthis
  • 169
  • 1
  • 4
  • 11
  • Why would you expect a windows command to be available on a unix platform? – Jenny D Jul 04 '16 at 10:50
  • This question is being voted for closure because the author does not show a level of technical understanding or appropriate due diligence in researching the topic that the community judges as being a minimum barrier to participate. – Jenny D Jul 04 '16 at 10:51
  • @JennyD I didn't know it was a Windows only command. That's basically the resolution to my question (and the explanation for why I couldn't find it in the docs). – emersonthis Jul 04 '16 at 20:38
  • @Jenny D says Reinstate Monica: Um, chdir IS a built-in command in C Shell. So yes, you could expect it show up across Linux/Unix. – Brian Preslopsky Dec 10 '19 at 19:41
  • @BrianPreslopsky The question is tagged `bash` and specified that it's about writing a bash script. – Jenny D Dec 10 '19 at 19:52

1 Answers1

3

Assuming you are using the bash shell, take a look at pushd and popd. These are two very unsung commands that are helpful in cases like this. pushd pushes the current working directory onto a stack and then does a cd to the directory you specify. popd will then pop the top directory from that stack and cd to it. Using these your alias would be:

alias vagrants_up=`pushd ~/Vagrant && vagrant up && popd`

These are very handy for interactive use, too. I alias cd to pushd and bk to popd and find that my workflow is less cluttered when I only have to think of going 'b'ac'k' one or two or three directory changes that I don't have to spell out rather than typing directory names all the time.

Greg Tarsa
  • 201
  • 2
  • 5