Ubuntu – How to change a directory using shell script

bashscripts

I am trying to change a directory to home/developer.
I used cd home/developer in my shell script.
After executing script it is again coming to original directory,where I executed shell script.

Best Answer

When you start your script a new process is created that only inherits your environment. Your current environment stays as it is. You can start your script like this if you want to change the current directory from a script:

. script.sh

or

source script.sh

The . (source is the long version of .) will evaluate the script in the current environment so it might be altered.

When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as script.sh, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

Sources:

Related Question