Does the change directory command cd work within a shell script ? I tried issuing a cd mydir within my script expecting it to chane to mydir directory.Nothing happens. Could somebody please explain ? How do I change to a directoy using shell ?
No you cannot if you're calling the script normally: your current shell would create a child-process, that would run the shell, change directory and exit. The parent process would not change directory.
One way to deal with this is to invoke the script from within your own shell. E.g.
. scriptname
The '.' would execute the script in your own process. Mind the use of 'exit' (and other such things) as they would end your current shell.
Many thanks Pieter.It works now.As per my observation if I execute a shell process,it creates a new child shell and remains in that child shell after completion of the script.I say this because if I execute the process as just scriptname , then to logout(exit from CRT) I need to type exit twice.But if I issue a . scriptname then I need to exit only once. Is this understanding of child shell correct?
If you start a normal program (not script) from a shell, a child process is used to run that program.
If you start a script, the script is examined to figure out which (shell) program to start. Subsequently, a child process is used to run that program (could be anything from bsh, sh, to perl or even a custom build program) and the script is executed by that process.
The . notation executes the script in your current 'shell'.
You are correct: If you do not leave the started (shell) program, you will have to 'exit' that shell, and the shell you were originally in.
When starting with the . notation, you do not start a second process, which thus does not have to be exited.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.