Reputation: 6451
Ok simple bash script question - don't laugh. My script just changes directory:
echo on;
echo "running script";
CURRENT_DIR=.;
cd ..;
pwd;
I can see it change directory in the echo but when it finishes it, my terminal is still at the same directory. Any tips?
Upvotes: 2
Views: 191
Reputation: 58224
When you run a bash
script, it runs in its own shell. That means that it has it's own shell environment including what the current working directory is. If you cd
within the script, that script will be operating in that new current directory. But when it's completed, you are still at the current directory that your user-level shell is at since a subshell doesn't touch it.
If you want to impact the current shell environment, one way is to execute it with .
:
. my_script
This is like running sh my_script
but operates within the environment of your current user shell.
Upvotes: 5