142 lines
6.8 KiB
HTML
142 lines
6.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>3.13.2 Shell variables and the environment</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value="3.13.2 Shell variables and the environment">
|
|
<meta name="keywords" value="gs">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3665 HREF="node152.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3663 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3657 HREF="node150.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3667 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3668 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3666 HREF="node152.html">3.13.2.1 The PATH environment </A>
|
|
<B>Up:</B> <A NAME=tex2html3664 HREF="node149.html">3.13 Customizing your Environment</A>
|
|
<B> Previous:</B> <A NAME=tex2html3658 HREF="node150.html">3.13.1 Shell scripts</A>
|
|
<BR> <HR> <P>
|
|
<H2><A NAME=SECTION005132000000000000000>3.13.2 Shell variables and the environment</A></H2>
|
|
<P>
|
|
<A NAME=4015> </A>
|
|
<A NAME=4016> </A>
|
|
<A NAME=4017> </A>
|
|
<A NAME=4018> </A>
|
|
The shell allows you to define <b>variables</b>, as most programming
|
|
languages do. A variable is just a piece of data which is given the
|
|
name.
|
|
<P>
|
|
<IMG ALIGN=BOTTOM ALT="(WARN)" SRC="bdt.gif">Note that Tcsh, as well as other C-type shells, use a
|
|
different mechanism for setting variables than is described here. This
|
|
discussion assumes the use of a Bourne shell, such as Bash (which you're
|
|
probably using). See the Tcsh man page for details.
|
|
<P>
|
|
When you assign a value to a variable (using the ``<tt>=</tt>'' operator),
|
|
you can access the variable by prepending a ``<tt>$</tt>'' to the
|
|
variable name, as demonstrated below.
|
|
<P><TT> /home/larry# <em>foo=``hello there''</em>
|
|
<P></TT>
|
|
The variable <tt>foo</tt> is given the value ``<tt>hello there</tt>''. You
|
|
can now refer to this value by the variable name, prefixed with a
|
|
``<tt>$</tt>'' character. The command
|
|
<P><TT> /home/larry# <em>echo $foo</em> <BR>
|
|
hello there <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
produces the same results as
|
|
<P><TT> /home/larry# <em>echo ``hello there''</em> <BR>
|
|
hello there <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
<P>
|
|
These variables are internal to the shell. This means that only the
|
|
shell can access these variables. This can be useful in shell scripts;
|
|
if you need to keep track of a filename, for example, you can store
|
|
it in a variable, as above. Using the command <tt>set</tt> will
|
|
display a list of all defined shell variables.
|
|
<P>
|
|
<A NAME=4035> </A>
|
|
<A NAME=4146> </A>
|
|
However, the shell allows you to <b>export</b> variables to the
|
|
<b>environment</b>. The environment is the set of variables which
|
|
all commands that you execute have access to. Once you define a variable
|
|
inside the shell, exporting it makes that variable part of the environment
|
|
as well. The <tt>export</tt> command is used to export a variable to
|
|
the environment.
|
|
<P>
|
|
<A NAME=4147> </A>
|
|
Again, here we differ between Bash and Tcsh. If you're
|
|
using Tcsh, another syntax is used for setting environment variables (the
|
|
<tt>setenv</tt> command is used). See the Tcsh man page for more information.
|
|
<P>
|
|
<A NAME=4042> </A>
|
|
The environment is very important to the UNIX system. It allows you
|
|
to configure certain commands just by setting variables which the
|
|
commands know about.
|
|
<P>
|
|
Here's a quick example. The environment variable <tt>PAGER</tt> is used
|
|
by the <tt>man</tt> command. It specifies the command to use to display
|
|
man pages one screenful at a time. If you set <tt>PAGER</tt> to be the
|
|
name of a command, it will use that command to display the man pages,
|
|
instead of <tt>more</tt> (which is the default).
|
|
<P>
|
|
Set <tt>PAGER</tt> to ``<tt>cat</tt>''. This will cause output from <tt>man</tt>
|
|
to be displayed to the screen all at once, without breaking it up into
|
|
pages.
|
|
<P><TT> /home/larry# <em>PAGER=``cat''</em>
|
|
<P></TT>
|
|
Now, export <tt>PAGER</tt> to the environment.
|
|
<P><TT> /home/larry# <em>export PAGER</em>
|
|
<P></TT>
|
|
Try the command <tt>man ls</tt>. The man page should fly past your screen
|
|
without pausing for you.
|
|
<P>
|
|
Now, if we set <tt>PAGER</tt> to ``<tt>more</tt>'', the <tt>more</tt> command
|
|
will be used to display the man page.
|
|
<P><TT> /home/larry# <em>PAGER=``more''</em>
|
|
<P></TT>
|
|
Note that we don't have to use the <tt>export</tt> command after we
|
|
change the value of <tt>PAGER</tt>. We only need to export a variable
|
|
once; any changes made to it thereafter will automatically be propagated
|
|
to the environment.
|
|
<P>
|
|
The man pages for a particular command will tell you if the command
|
|
uses any environment variables; for example, the <tt>man</tt> man page
|
|
explains that <tt>PAGER</tt> is used to specify the pager command.
|
|
Some commands share environment variables; for example, many commands
|
|
use the <tt>EDITOR</tt> environment variable to specify the default editor
|
|
to use when one is needed.
|
|
<P>
|
|
The environment is also used to keep track of important information
|
|
about your login session. An example is the <tt>HOME</tt> environment
|
|
variable, which contains the name of your home directory.
|
|
<P><TT> /home/larry/papers# <em>echo $HOME</em> <BR>
|
|
/home/larry
|
|
<P></TT>
|
|
<P>
|
|
Another interesting environment variable is <tt>PS1</tt>, which defines
|
|
the main shell prompt. For example,
|
|
<P><TT> /home/larry# <em>PS1=``Your command, please: ''</em> <BR>
|
|
Your command, please:
|
|
<P></TT>
|
|
To set the prompt back to our usual (which contains the current
|
|
working directory followed by a ``<tt>#</tt>'' symbol),
|
|
<P><TT> Your command, please: <em>PS1=``<code>\w# </code>''</em> <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
The <tt>bash</tt> man page describes the syntax used for setting the prompt.
|
|
<P>
|
|
<BR> <HR>
|
|
<UL>
|
|
<LI> <A NAME=tex2html3669 HREF="node152.html#SECTION005132100000000000000">3.13.2.1 The PATH environment variable</A>
|
|
</UL>
|
|
<BR> <HR><A NAME=tex2html3665 HREF="node152.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3663 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3657 HREF="node150.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3667 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3668 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3666 HREF="node152.html">3.13.2.1 The PATH environment </A>
|
|
<B>Up:</B> <A NAME=tex2html3664 HREF="node149.html">3.13 Customizing your Environment</A>
|
|
<B> Previous:</B> <A NAME=tex2html3658 HREF="node150.html">3.13.1 Shell scripts</A>
|
|
<BR> <HR> <P>
|
|
<BR> <HR>
|
|
<P><ADDRESS>
|
|
<I>Matt Welsh <BR>
|
|
mdw@sunsite.unc.edu</I>
|
|
</ADDRESS>
|
|
</BODY>
|