How to run 'cd' in shell script and stay there after script finishes?

Viewed 73572

I used 'change directory' in my shell script (bash)

#!/bin/bash
alias mycd='cd some_place'
mycd
pwd

pwd prints some_place correctly, but after the script finished my current working directory doesn't change.

Is it possible to change my path by script?

7 Answers

Another practical solution is to end your script by opening another shell session. For instance:

#!/bin/bash
cd some_place
bash

This is useful, in my case, for scripts located in my ~/bin for instance, called from any other place. It is just a bit painful to type source ~/bin/mygoodoldscript instead of mygoo<TAB><ENTER>.

The downside is that the additional shell takes up a few more resources (not much).

Though there are answers. I think the intention of question is to use script to navigate to specific path.

Here is a simple practical solution works here without cancel out existing terminal environment flag.

  1. provide a bash/tch/sh script to work for path generation
/* .goto.sh */
#!/usr/bin/env bash
echo '~/workspace'
  1. add alias to the script output
alias goto 'cd `.goto.sh`'
Related