Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
possible duplicate of Why doesn't "cd" work in a shell script? –  muru Dec 4 at 18:16

1 Answer 1

up vote 8 down vote accepted

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:

share|improve this answer
    
If I run the mentioned command,terminal is getting close. –  Sujatha Jul 29 '13 at 7:30
2  
@Sujatha Can you post your script on paste.ubuntu.com ? I suspect that you have exit command inside the script. If so, it is normal. –  Radu Rădeanu Jul 29 '13 at 7:32
    
Thank you. It is working fine. I have uncommented the exit command. –  Sujatha Jul 29 '13 at 7:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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