Alex Rivera | Logout

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

Asked 2010-10-07T07:13:44.337
97

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?

Edit
Report

1 Answer

108

You need to source the file as:

. myfile.sh

or

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.

answered 2010-10-07T07:17:50.683

Your Answer