Bourne Shell Usage
Part II

Objective of this class

  • What are environment variables
  • What are aliases
  • Functions
  • How does it startup
  • Differences between shells
  • Shell scripting
  • Home Work

What are environment variables

What environment variable tells you which shell you are using?
How can you set environment variables under Windows?
Who can tell me what the environment variable PS1 is used for?

Environment variables are defined as:
*a variable that defines an aspect of your working environment such as your home directory, editor or printer. These variables are set during the login procedure.“*

Frequently used Environment Variables
Name          Description
EDITOR        Sets the editor that will be used by other programs
              such as the mail program.

PAGER         Sets the pager that will be used by programs such as
              man to display man pages.

PATH          Specifies the directories that the shell is to look
              through to find a command. These directories are
              searched in the order in which they appear.

PRINTER       Sets the printer to which all output is sent by the lpr
              command

SHELL         Sets your default shell.

TERM          Sets your terminal type for programs such as the
              editor and pager.

TZ            Sets the time zone you are in.

Have a look at Environments of Processes. to learn more about Environment variables

As a point of reference, the information presented in Reference page, Rute User’s Tutorial and Exposition is an excellent source of information.

What are aliases

The shell function Alias allows you to create a shortcut for a command line. Lets look at some examples to show the idea.

588 [MooreJ@MOOREJ] /usr/local/etc
 % ls
ask.pl  im_palette-small.pal  im_palette.pal  mc.global
gtk     im_palette-tiny.pal   imrc

589 [MooreJ@MOOREJ] /usr/local/etc
 % ls -a
.   ask.pl  im_palette-small.pal  im_palette.pal  mc.global
..  gtk     im_palette-tiny.pal   imrc

590 [MooreJ@MOOREJ] /usr/local/etc
 % ls -F
ask.pl*  im_palette-small.pal  im_palette.pal  mc.global
gtk/     im_palette-tiny.pal   imrc

591 [MooreJ@MOOREJ] /usr/local/etc
 % which ls
/usr/bin/ls

592 [MooreJ@MOOREJ] /usr/local/etc
 % alias lc="/usr/bin/ls -aF"
593 [MooreJ@MOOREJ] /usr/local/etc
 % lc
./   ask.pl*  im_palette-small.pal  im_palette.pal  mc.global
../  gtk/     im_palette-tiny.pal   imrc

I typed the ls command in the /usr/local/etc directory under Cygwin. I then added the option -a to see all the files, even the . files. Next I tried the -F option to see which were files, which were directories and which were executable. I then located the ls command with which ls , and created an alias alias lc=“/usr/bin/ls -aF” . Finally I used the lc command.

I can type the command alias by it’s self to see what aliases are defined.

Functions

This is a more advanced type of alias which allows variables passed in. Lets look at an example to make it clearer.

549 [EXMXM@MOOREJ] /cygdrive/d/home/john
 % find ~ -type f -name .emacs -print
/cygdrive/d/home/john/.emacs

550 [EXMXM@MOOREJ] /cygdrive/d/home/john
 % function ff () {
> find ~ -type f -name $1 -print
> }

551 [EXMXM@MOOREJ] /cygdrive/d/home/john
 % ff .emacs
/cygdrive/d/home/john/.emacs

How does it startup

A shell is started when you login to a Unix box. It then reads some configuration files to know what to do. Lets look at the man page for the Bash Shell

Basically the shell looks for the files /etc/profile, .profile, .bash_profile and executes them when it starts. Another way to initialize things in your environment is the .bashrc file. This file is used to initialize the environment when ever a new shell is spawned.

Notice the name of this file .bashrc . This is the typical name for a runtime configuration file. Unix programs which take options often use this type of file as it’s configuration. The . (dot) at the beginning mean the file name is hidden. Lets see how this works on TSWTools.

    111 john[pts/1] % ls /home/siggy                                   /zip/lessons
    Desktop  edit  tty5
    112 john[pts/1] % lc /home/siggy                                   /zip/lessons
    ./     ../     .bash_history  .bash_logout  .bash_profile  .bashrc  Desktop/
    edit/  .emacs  .kde/          .kderc        .screenrc      tty5

Differences between shells

This page, Command and Shell User’s Guide , which we saw the first lesson, gives some good information. There is also the page, Summary of shell facilities which compares the differences between shells.

In no way is this a complete introduction to shells. I have given you the tools to get going. From here on you should learn to look things up, and ask questions.

Shell Scripting

Lets try some scripting on the spot. Have a look at: Creating and executing simple shell scripts for more information.

Home Work

Create a home directory on your system. Create a .bash_profile and .bashrc files. These will allow you to play with the variables.



Written by John F. Moore

Last Revised: Mon Jan 16 15:37:34 EST 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