View Single Post

   
  #2 (permalink)  
Old 02-20-2008, 10:53 PM
Joost Kremers
 
Posts: n/a
Default Re: shell script question

George wrote:
> This is probably a simple question. How can you get the current terminal
> to reside in a directory specified in a .sh file.
>
> Example:
>
> #!/bin/bash
> cd /home/da/projects;
>
>
> The terminal is still in the same directory path as it was before
> calling the script. How can you use a script to change the current
> directory in your terminal window?


a script is always run in its own environment (i.e. its own instance of the
shell). this is to make sure that the calling shell cannot be influenced by
the script. (that would be a serious security risk.)

the way to get what you want is to source the script, instead of running
it. if your script is /home/user/bin/script.sh, then

user@computer:~$ source bin/script.sh

will do what you want. the source command can be abbreviated with a dot:

user@computer:~$ . bin/script.sh

note, however, that if you source a script, *everything* that that script
does is done in the calling environment. that includes variables being set
or reset.

--
Joost Kremers joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
Reply With Quote