Department of Engineering

IT Services

Shell Creation

Whenever you invoke a command that is not built into the shell, a new shell process is created which inherits many of the properties of its parent. However, variables and aliases are not inherited unless they are exported. Type ``export'' and you will see what's been exported by the initialisation files. Typing

      she=janet ; he=john ; export she

(`;' is a statement separator) creates 2 new variables, only one of which is exported. They can be printed out using

      echo $she $he

Now type ``bash''. This will put you into a new shell, a child of the original.

      echo $she $he

will only print out one name, the one you exported. If you type ``ps -f'' you will get an output something like

tpl       6006 31173  0 14:03 pts/3    00:00:00 bash
tpl       6027  6006  0 14:03 pts/3    00:00:00 ps -f
tpl      31173 31172  0 09:01 pts/3    00:00:00 -bash

Notice that you are running 2 bash processes. The PPID column gives the PID of the parent process so you can see that the 2nd shell was started from within the 1st. You can kill this new shell by typing exit. More interestingly, you can suspend the shell by typing <CTRL> Z and fall back into the original shell. Convince yourself that this is so by trying the `echo' and `ps' commands again, or `jobs', then return to the child shell by typing `fg' (foreground). You won't often need to jump around like this but it's useful to be aware of the mechanism, especially since it helps elucidate some problems regarding scope and lifetimes of variables.

When you run a script by typing its name, a new process is started up. If the script contains a cd command or changes the value of an environmental variable, only the new process will be affected - the original shell's environment and current directory won't have been 'overwritten'. If however you type 'source scriptname' then the commands in the script are run in the original process. This is called sourcing the script.

You can force shell script commands to run in a new process by using brackets. For example, if you type ``cd / ; ls'' you will be left in the root directory, but if you type ``(cd / ; ls)'' you'll end up where you began because the cd command was run in a new, temporary, process.