qrtt1 qrtt1 - 1 year ago 72
Bash Question

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

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

alias mycd='cd some_place'

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

Is it possible to change my path by script?

Answer Source

You need to source the file as:

. myfile.sh


source myfile.sh

Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download