Bourne Shell Usage

Objective of this class

  • What is a shell
  • Commands and assignment
  • Characters with special meaning
  • Redirection and Parameter Substitution
  • What are environment variables
  • What are aliases
  • How does it startup
  • Differences between shells
  • Shell scripting
  • Home Work

What is a shell

Today we are going to discuss the Bourne shell. This shell is the original shell program on all unix systems. It is common on most unix systems to create all the shell scipts for this shell. We will see how to specify this later.

Lets begin by taking a look at the man page for the Bourne shell as taken from the manpages. Here is the Bash man page. If you would like to look up man pages on the internet take a look at Linux Man Pages at Kansus State University.

Commands and assignments

       A simple-command is a sequence of  non-blank  words  sepa-
       rated by blanks.  The first word specifies the name of the
       command to be executed.  Except as  specified  below,  the
       remaining  words  are  passed  as arguments to the invoked
       command.  The command name is passed as  argument  0.

This specifies how the command line arguments are interperted. Each argument on the command line is placed in a variable. The variables are listed as $0, $1, $2, … $9. The way this works is that each argument on the command line is stored in one of these variables. For example the I created a shell script to output the command line arguments using the borne shell.

./command-line.sh first second third fourth fifth

Argument $0 = ./command-line.sh
Argument $1 = first
Argument $2 = second
Argument $3 = third
Argument $4 = fourth
Argument $5 = fifth
Argument $6 = 
Argument $7 = 
Argument $8 = 
Argument $9 = 

Keep this argument idea in mind, we will return to it in Parameter Substitution later.

Characters with special meaning

The shell has some symbols it assigns special meaning to.

Symbol Meaning

<

This is the symbol used to control STDIN. For example mail John <command-line.sh would take the contents of the shell script command-line.sh and email it to John.

>

This is the symbol used to control STDOUT. This program is often used to save the output of a program to a file. ls >boo , would save the output of ls in the file boo overwriting what ever it contain previously. Or if I did ls >>boo it would append the output of ls to the file boo.

;

This symbol is used to end a command. This symbols allows me to enter multiple commands on the same line. pwd ; ls ; du -k would list the current directory name, then it’s contents, then it’s size in kilobytes.

&

This symbol is used to tell the shell not to wait for the operation to complete. When we import a database we use the command xterm & which starts an xterm and passes control back to the original shell immediately instead of waiting for the xterm to exit.

|

This symbol is used to connect STDIN of one program to STDOUT of another. This command allows us to stack up commands by redirecting STDIN and STDOUT. For example the command ps -ef | grep -i httpd would select the web servers from the list of running tasks.

#

This begins comments so you can place comments in shell scripts. This allows you to embed comments in files which are there to read, but are not executed. Take a look at the shell script used above command-line.sh to see how it contains comments.

\

This is called the Quote character. Any character following it looses it’s special meaning. This is used to display a character instead of interpert it. For example:

           504 [MooreJ@MOOREJ] /
           % echo #comment here

           505 [MooreJ@MOOREJ] /
           % echo \#comment here
           #comment here

?

This is a single wild card character. Suppose you had the following files in you directory: boo.1 boo.10 boo.100. If you wanted to find them all you could type ls boo.* But if you only wanted the first one you could type ls boo.?

529 [MooreJ@MOOREJ] /tmp
% touch boo.1 boo.10 boo.100

530 [MooreJ@MOOREJ] /tmp
% ls boo.*
boo.1  boo.10  boo.100

531 [MooreJ@MOOREJ] /tmp
% ls boo.?
boo.1

532 [MooreJ@MOOREJ] /tmp
% ls boo.??
boo.10

*

This is a wild card, just as in Dos.

` (back tic)

This symbol works in pairs to tell a shell to execute what is between the symbols. For example the command which vi returns the location of the vi command. So file `which ls` would take the output of the which vi command and tell you the file type.

 521 [MooreJ@MOOREJ] /tmp
 % which vi
 /usr/bin/vi

 522 [MooreJ@MOOREJ] /tmp
 % file /usr/bin/vi
 /usr/bin/vi: symbolic link

 523 [MooreJ@MOOREJ] /tmp
 % file \`which vi\`
 /usr/bin/vi: symbolic link

Redirection and Parameter Substitution

A program running in a shell had three location predefined for it they are called STDIN, STDOUT, and STDERR. They are known as Standard Input, Standard Output, and Standard Error. These directions are assigned by the shell. So to use a pipe, the shell simply connects the STDOUT of one program to the STDIN of another.

       The character $ is used to introduce substitutable parame-
       ters.  There are two types of parameters,  positional  and
       keyword.   If  parameter  is  a  digit, it is a positional
       parameter.  Positional parameters may be  assigned  values
       by  set.  Keyword parameters (also known as variables) may
       be assigned values by writing:

             name=value [ name=value ] ... 

This refers back to our previous discussion of $0, $1, etc. The shell allows you to assign values to variables which are available to programs.

OK, OK, what good is this? Many programs use these variables in the shell to set configuration information. For example the command man , reads a variable called PAGER to find out what pager to use with man pages.

What are environment variables

When you run a shell you can configure some variables to pass information to programs. We just saw an example above with PAGER . Notice something I typed PAGER , not pager . This is because Unix is CASE SENSITIVE .

          544 [MooreJ@MOOREJ] /tmp
           % echo $PAGER
          /usr/bin/less
          545 [MooreJ@MOOREJ] /tmp
           % echo $pager

          546 [MooreJ@MOOREJ] /tmp
           %

This is why the Centaur password is ROUTINE and routine does not work.

You can view individual environment variables using the echo command, as seen above. If you want to see all the variables you can use the command env to display the whole list.

To set an environment variable using the command export

          549 [MooreJ@MOOREJ] /tmp
           % export BOO="Hello World"
          550 [MooreJ@MOOREJ] /tmp
           % echo $BOO
          Hello World

Home work

Spend some time in the shell and try viewing, and setting environment variables.

Next week we will discuss how to configure your startup files for a shell.



Written by John F. Moore

Last Revised: Wed Oct 18 11:01:31 EDT 2017

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
HTML5 Powered with CSS3 / Styling, and Semantics