The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 03-08-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 974
Quote:
Originally Posted by SkySmart
is there anyway to change directories in a script and have it actually work?

when u put something like cd /var/tmp in a script named runner.

when u run that runner script, the current directory is still what it is. it doesn't change to /var/tmp.

what can be done about this?
When you execute a script, it is run in a child process, and a child process cannot affect the environment of its parent.

To change directories, you need to execute cd in the current shell; cd is a shell builtin command, and thus runs in the current shell environment.

To get a script to run in the current shell, you can source it, using the dot command:

Code:
. name-of-script
In bash, you can also use the word, source:

Code:
source name-of-script
You can also put it into a function.
Reply With Quote