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:
In bash, you can also use the word, source:
Code:
source name-of-script
You can also put it into a function.