Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing to a directory using shell 1

Status
Not open for further replies.

areza123

Programmer
Jul 4, 2002
60
IN
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top