next up previous contents
Next: Redirection and Pipelines Up: The Bourne Shell Previous: Getting to Know the

Multiple Commands Per Line

One of the things that the Bourne shell looks for when a command is entered, is if there is more than one command entered. The most common way to have multiple commands entered on one line is to use the semicolon separator. This separator tells the shell to send each command to be processed in the order in which they appear, like the following:

$ cd docs; mkdir old_docs; mv *.* old_docs <enter>

which is the same as

$ cd docs <enter> 
$ mkdir old_docs <enter> 
$ mv *.* old_docs <enter>

Now suppose there is a situation where a user wants to have some commands carried out at the same time without having to wait for each to start. A particular situation that comes to mind is copying a several megabyte file (which can take minutes) while trying to do anything else. The following could be entered:

$ cp big_file new_dir& rm *.o& who <enter>

which is equivalent to

$ cp big_file new_dir& <enter>
$ rm *.o& <enter> 
$ who <enter>

where the shell puts the command (plus arguments) before an ampersand into the background. In the above case it copies the big_file (in the background), it deletes all of the object files (in the background) and finally it runs the who command (in the foreground)gif.

The Bourne shell also allows command grouping. Command grouping treats a group of commands as a unit and executes them as such in a subshell. To group commands enclose them in round parentheses () and separate the commands by semicolons. The grouped commands are executed in a subshell. For example,

$ MY_name='Norm Buchanan'
$ (MY_name='George Bush'; echo $MY_name)
George Bush
$ echo $MY_name
Norm Buchanan

This example was chosen to demonstrate how command grouping works, but also to give a glimpse of variable scope or visibility (which will be covered in greater detail in section 2.7, variables). User defined variables are local to the current shell which means that they can not be accessed or altered in a subshell. This is why the variable MY_name returns to its original value as soon as the commands enclosed by the parentheses have been executed.

These features lead quite naturally into more powerful features of the Bourne shell, or shells in general.

next up previous contents
Next: Redirection and Pipelines Up: The Bourne Shell Previous: Getting to Know the

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998