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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing directory

Status
Not open for further replies.

spuppett

Programmer
Jan 25, 2003
48
US
OK, Im new to UNIX, but so far i am diggin it. I've written a couple of scripts so far, but right now I am stuck on one that shouldn't be hard at all. All i want to do is type in my command and change to the specified directory. My script looks like this:

#!/bin/sh

cd /user/spuppett/Windows_NT/Desktop/LinuxStuff/

#end

when i run it, it does nothing. Whats wrong with it?
 
You would have to &quot;source&quot; the script for the parent shell to change to that directory too. For instance, to run it, you'd execute [tt]. /path/to/script[/tt] or [tt]source /path/to/script[/tt] instead of the usual [tt]/path/to/script[/tt] or [tt]<interpreter> /path/to/script[/tt]. //Daniel
 
sorry man, that didn't help me at all, don't know what you mean. sorry.

Thanks for trying though.
 
Here's what happens when you execute your current script.
The shell you are executing it from (the &quot;parent&quot;) executes whatever program is on the shebang line (in your case [tt]/bin/sh[/tt]) with the script name as a parameter. This creates a new instance of the shell. So when the &quot;child&quot; (the program started by the &quot;parent&quot;) gets that script name as a parameter, it reads through the file and executes the commands. When it is done, it terminates and all settings are lost. The control is then handed over to the &quot;parent&quot;, which has no idea what the &quot;child&quot; just did, all it knows about it is the exit status.
Now, if you instead &quot;[tt]source[/tt]&quot; the script, the &quot;parent&quot; itself reads the file and executes the command, meaning that the settings and changes are still there when the script ends. This is what you want to do. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top