Subject: Using cd in scripts
From: Josh Smith (email@example.com)
Date: Thu Jun 15 2000 - 10:15:40 MDT
JBS> So, when you run a shell script (or a Perl script, or whatever) that
JBS> changes your CWD, it changes it within the context of that process, and
JBS> when that process exits, and you return to the shell
Just in case this wasn't obvious: When you run a shell script, you are in
fact starting a new process, which runs the commands in the script file, and
then exits. This may not be how .BAT scripts work in DOS (I don't actually
know DOS, so I'm not sure).
There's another thing you can do: You can use the 'source' command to run
the commands in a file, within the context of the current process (i.e. it
does not create a new process). So given a file commands.txt containing
if you do
to run the script, it will create a new shell process, which will change to
/var/tmp, touch a file called foo, and then exit, returning you to whatever
directory you were in when you ran the script. Whereas if you do
bash% source commands.txt
it will do the same thing, but without creating a new shell (i.e. within the
context of your current shell), so it'll leave your current shell in
/var/tmp when you're done.
Which one you want depends on exactly what effect you're trying to
accomplish. :^) Aliases are better if you only want to run one command, but
if you need to do more than one thing at a time, you can write a script that
you source rather than executing. And you can combine these two, e.g.
alias dostuff='source $HOME/bin/stuff-to-do.txt'
will let you run a bunch of commands in the context of your current shell
with a single command.
This archive was generated by hypermail 2a24 : Thu Jun 15 2000 - 10:17:58 MDT