
\documentclass[11pt]{article}

\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.0in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.12in}

\usepackage{times}      

\begin{document}

\newcommand{\bfs}[1]{{\bf #1}}

\title{A Brief Introduction to the Use of Shell Variables}

\author{Norman Matloff}

\date{July 25, 2001}

\maketitle

\tableofcontents

\section{Two Popular Shells:  tcsh and bash}

\subsection{Overview}

There are many different shells available for Unix systems.  Here we
focus on two of the most popular ones, {\bf tcsh} and {\bf bash}.  Other
shells tend to be very similar to one or both of these.

Your Unix account has a preset login shell for you, typically {\bf tcsh}
in academic settings and {\bf bash} in Linux.  When you log in, a shell
of the given type is automatically started for you.  (This can be seen
in the file /etc/passwd.) If you wish to temporarily run a different
shell, just type its name.  If you wish to change your login shell, run
the {\bf chsh} command.

\subsection{Command Commonality Among Shells}

Most of what people tend to think of as ``basic Unix commands'' are
actually shell commands which are common to almost all of the shells.
So for example you could type

\begin{verbatim}

cd /abc/def
\end{verbatim}

to change to the directory /abc/def in either {\bf tcsh} or {\bf
bash}.\footnote{Technically speaking, the ``change directory'' command
for Unix itself is chdir(), a function which must be called from a
program.  A shell is a program, and when for example you issue the {\bf
cd} command to a shell, that program in turn calls chdir().}

\section{Shell Variables}

\subsection{Introduction to Shell Variable Syntax}

The {\bf tcsh} shell uses ``set'' and = for the assignment operation.
For example, 

\begin{verbatim}

set x = 3
\end{verbatim}

would assign the value 3 to the shell variable x.  In {\bf bash}, this
would be

\begin{verbatim}

x=3
\end{verbatim}

(The spaces around `=' are optional in {\bf tcsh} but illegal in {\bf
bash}.

When using a shell variable, a dollar sign must be prepended.  In the
{\bf tcsh} example above, for instance, if we want to add 12 to x and
set y equal to the sum, we must write

\begin{verbatim}

set y = $x + 12
\end{verbatim}

not

\begin{verbatim}

set y = x + 12
\end{verbatim}

Many shell variables consist of arrays of strings.  To add one more
string to such a variable, {\bf tcsh} uses parentheses while {\bf bash}
uses the : operator.

A special kind of shell variables is {\it environment variables}.  If
you set one of these from a shell and then use the shell to run a
program, that program will inherit all the values of the environment
variables.

Say for example you run the program z from {\bf tcsh}.  If you first
type

\begin{verbatim}

setenv x 3
\end{verbatim}

the variable x, with its value 3, will be available to z.  In {\bf
bash}, this is done by typing

\begin{verbatim}

export x=3
\end{verbatim}

\subsection{Some Important Shell Variables}

In this section we note some examples of important built-in shell
variables.  In each subsection title, we give the {\bf tcsh} variable
first, and then its {\bf bash} equivalent.

\subsection{\$cwd, \$PWD}

This variable consists of a string which records the name of the current
directory.

Typing

\begin{verbatim}
set cwd = b
\end{verbatim}

in {\bf tcsh} would have the same effect as

\begin{verbatim}
cd b
\end{verbatim}

\subsection{\$path, \$PATH}

This is an extremely important variable.  When you issue a command to
the shell, the shell will search through various directories to find
the executable file for that command, so that the command can be run.

A typical value for \$path might be

\begin{verbatim}

. /usr/local/bin /usr/ucb /usr/bin /usr/etc /etc /bin /usr/bin/X11 
\end{verbatim}

Suppose we give the shell the command z.  The shell will first search for
a file named z in our current directory (`.'); if not found there, the
shell will next look for the file in the directory /usr/local/bin; and
so on.  

If you create a new directory in which you put executable files which
you use often, you should add this directory to your path, so that you
can conveniently execute the programs from any other directory.  Suppose
the full name of z is /a/b/c/z.  Then to add the directory /a/b/c to
your the end of your path in {\bf tcsh}, type

\begin{verbatim}

set path = ( $path /a/b/c )
\end{verbatim}

This concatenates the old value of \$path, which was an array of
strings, with one more string, ``/a/b/c'', and assigns the result back
to the variable \$path.  You may now simply type ``z'' to execute z, no
matter which directory you are in.

The same action in {\bf bash} would be accomplished by using the :
operator, i.e.

\begin{verbatim}

PATH=$PATH:/a/b/c
\end{verbatim}

By the way, if you add a new program to your system, or use {\bf mv} to
rename it, you probably will then need to run {\bf rehash} to let your
shell know that the set of executables it found before needs to be
updated.

\subsection{\$term, \$TERM}

This one is also quite important.  Without it, programs like \bfs{emacs},
\bfs{vi}, \bfs{talk}, etc. would not know your terminal type, and would
be useless.

\section{Aliases}

You may wish to invent your own shell commands, which you can do with
{\it aliases}.

For example, here is one I use in {\bf tcsh}:

\begin{verbatim}

alias ls "ls -F"
\end{verbatim}

That means that the ordinary {\bf ls} command will always be replaced by
ls -F, which I prefer because of its richer information content.

Another common example:

\begin{verbatim}

alias mroe more
\end{verbatim}

I often mistype the {\bf more} command as ``mroe,'' so this
automatically rectifies my error whenever I make this mistake.

In {\bf bash}, these examples would be written as 

\begin{verbatim}

alias ls='ls -F'
alias mroe=more
\end{verbatim}

\section{Startup Files}

To explain the idea of {\it startup files}, let's start with {\bf tcsh}.

When you run {\bf tcsh} (or it is automatically run for you, when you
first log in), {\bf tcsh} will first execute whatever {\bf tcsh}
commands are in some system file (/etc/csh.cshrc in the case of Linux).
This will set \$path to a basic value common to all users, and set some
other variables as well.  

But you may wish to have \$path also include a few more directories that you
often use, /a/b/c.  As noted earlier, you could do this by manually
typing

\begin{verbatim}

set path = ( /a/b/c $path )
\end{verbatim}

But you can automate this by including that line in a file .tcshrc in
your home directory.  When {\bf tcsh} starts, after checking
/etc/csh.cshrc (or whatever other default is set up on your system),
it will then check .tcshrc in your home directory, so the above path
extension will automatically be done, a convenience to you.

(If there is no .tcshrc file, {\bf tcsh} will then check for a file
.cshrc in your home directory.  The latter file is also used by {\bf
tcsh}'s ancestor, {\bf csh}, so I recommend you use this file, thus
making it available to both shells.) 

One usually puts a lot of aliases in shell startup files too.

The analogous files for {\bf bash} are /etc/profile, .bashrc and
.profile, respectively.

\end{document}




