This question already has an answer here:

here is a small test I ran, to try and move me back to root:

cd /

It returns the current working directory, and then the correct new one, but when the script finished executing, I am still in the original directory and haven't moved. Is there any way to achieve this?


marked as duplicate by Michael Homer, Costas, Anthon, slm Apr 16 '15 at 12:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Cheers Michael! – CYQ00000A Apr 16 '15 at 9:58

There is no way to achieve this within a script - scripts start subshell, which is a standalone environment. There's all sorts of reasons for this, but pretty fundamentally - a script cannot tamper with your environment (including your cwd).

The closest you get is creating an alias within the current shell.

alias chr="cd /"

Either that, or 'source' the script, it "runs" in the current shell. E.g. like your .bashrc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.