vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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? Thanks |
| |||
| 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) |
| |||
| Joost Kremers wrote: > 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. > Awsome! That worked great, thanks! |
| ||||
| Joost Kremers wrote: > 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. > And that is also true for an 'exit' in the script: it will end the calling shell, which is probably your login shell. It should be clear that most scripts are not suitable to be sourced. The source command is useful, and commonly used, to read parameters from a configuration file. See /etc/rc.d/rc.inet1.conf as an example which is sourced by /etc/rc.d/rc.inet1. Regards, Kees. -- Kees Theunissen. |