slackware-current/slackbook/html/shell-bash.html
Patrick J Volkerding 75a4a592e5 Slackware 13.37
Mon Apr 25 13:37:00 UTC 2011
Slackware 13.37 x86_64 stable is released!

Thanks to everyone who pitched in on this release: the Slackware team,
the folks producing upstream code, and linuxquestions.org for providing
a great forum for collaboration and testing.

The ISOs are off to be replicated, a 6 CD-ROM 32-bit set and a
dual-sided
32-bit/64-bit x86/x86_64 DVD.  Please consider supporting the Slackware
project by picking up a copy from store.slackware.com.  We're taking
pre-orders now, and offer a discount if you sign up for a subscription.

As always, thanks to the Slackware community for testing, suggestions,
and feedback.  :-)

Have fun!
2018-05-31 22:45:18 +02:00

234 lines
8.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title>The Bourne Again Shell (bash)</title>
<meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" />
<link rel="HOME" title="Slackware Linux Essentials" href="index.html" />
<link rel="UP" title="The Shell" href="shell.html" />
<link rel="PREVIOUS" title="The Command Line" href="shell-command-line.html" />
<link rel="NEXT" title="Virtual Terminals" href="shell-vt.html" />
<link rel="STYLESHEET" type="text/css" href="docbook.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"
alink="#0000FF">
<div class="NAVHEADER">
<table summary="Header navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<th colspan="3" align="center">Slackware Linux Essentials</th>
</tr>
<tr>
<td width="10%" align="left" valign="bottom"><a href="shell-command-line.html"
accesskey="P">Prev</a></td>
<td width="80%" align="center" valign="bottom">Chapter 8 The Shell</td>
<td width="10%" align="right" valign="bottom"><a href="shell-vt.html"
accesskey="N">Next</a></td>
</tr>
</table>
<hr align="LEFT" width="100%" />
</div>
<div class="SECT1">
<h1 class="SECT1"><a id="SHELL-BASH" name="SHELL-BASH">8.3 The Bourne Again Shell
(bash)</a></h1>
<div class="SECT2">
<h2 class="SECT2"><a id="SHELL-BASH-ENVIRONMENT" name="SHELL-BASH-ENVIRONMENT">8.3.1
Environment Variables</a></h2>
<p>A Linux system is a complex beast, and there's a lot to keep track of, a lot of little
details that come into play in your normal interactions with various programs (some of
which you might not even need to be aware of). Nobody wants to pass a bunch of options to
every program that gets run, telling it what kind of terminal is being used, the hostname
of the computer, how their prompt should look...</p>
<p>So as a coping mechanism, users have what's called an environment. The environment
defines the conditions in which programs run, and some of this definition is variable;
the user can alter and play with it, as is only right in a Linux system. Pretty much any
shell will have environment variables (if not, it's probably not a very useable shell).
Here we will give an overview of the commands bash provides for manipulating its
environment variables.</p>
<p><tt class="COMMAND">set</tt> by itself will show you all of the environment variables
that are currently defined, as well as their values. Like most <tt
class="COMMAND">bash</tt> built-ins, it can also do several other things (with
parameters); we'll leave it to the <tt class="COMMAND">bash</tt>(1) man page to cover
that, though. <a href="shell-bash.html#EX-SHELL-BASH-ENVIRONMENT">Example 8-1</a> shows
an excerpt from a <tt class="COMMAND">set</tt> command run on one of the author's
computers. Notice in this example the <tt class="ENVAR">PATH</tt> variable that was
discussed earlier. Programs in any of those directories can be run simply by typing the
base filename.</p>
<div class="EXAMPLE"><a id="EX-SHELL-BASH-ENVIRONMENT"
name="EX-SHELL-BASH-ENVIRONMENT"></a>
<p><b>Example 8-1. Listing Environment Variables with <tt
class="COMMAND">set</tt></b></p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">set</kbd>
PATH=/usr/local/lib/qt/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:
/usr/openwin/bin:/usr/games:.:/usr/local/ssh2/bin:/usr/local/ssh1/bin:
/usr/share/texmf/bin:/usr/local/sbin:/usr/sbin:/home/logan/bin
PIPESTATUS=([0]="0")
PPID=4978
PS1='\h:\w\$ '
PS2='&#62; '
PS4='+ '
PWD=/home/logan
QTDIR=/usr/local/lib/qt
REMOTEHOST=ninja.tdn
SHELL=/bin/bash
</pre>
</td>
</tr>
</table>
</div>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">unset <var
class="REPLACEABLE">VARIABLE</var></kbd>
</pre>
</td>
</tr>
</table>
<p><tt class="COMMAND">unset</tt> will remove any variables that you give it, wiping out
both the variable and its value; <tt class="COMMAND">bash</tt> will forget that variable
ever existed. (Don't worry. Unless it's something you explicitly defined in that shell
session, it'll probably get redefined in any other session.)</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">export <var
class="REPLACEABLE">VARIABLE=some_value</var></kbd>
</pre>
</td>
</tr>
</table>
<p>Now, <tt class="COMMAND">export</tt> is truly handy. Using it, you give the
environment variable <tt class="ENVAR">VARIABLE</tt> the value &#8220;<var
class="LITERAL">some_value</var>&#8221;; if <tt class="ENVAR">VARIABLE</tt> didn't exist,
it does now. If <tt class="ENVAR">VARIABLE</tt> already had a value, well, it's gone.
That's not so good, if you're just trying to add a directory to your <tt
class="ENVAR">PATH</tt>. In that case, you probably want to do something like this:</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">export PATH=$PATH:<var
class="REPLACEABLE">/some/new/directory</var></kbd>
</pre>
</td>
</tr>
</table>
<p>Note the use of <tt class="ENVAR">$PATH</tt> there: when you want <tt
class="COMMAND">bash</tt> to interpret a variable (replace it with its value), tack a
<var class="LITERAL">$</var> onto the beginning of the variable's name. For instance, <tt
class="COMMAND">echo $PATH</tt> will echo the value of <tt class="ENVAR">PATH</tt>, in my
case:</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">echo $PATH</kbd>
/usr/local/lib/qt/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:
/usr/openwin/bin:/usr/games:.:/usr/local/ssh2/bin:/usr/local/ssh1/bin:
/usr/share/texmf/bin:/usr/local/sbin:/usr/sbin:/home/logan/bin
</pre>
</td>
</tr>
</table>
</div>
<div class="SECT2">
<h2 class="SECT2"><a id="SHELL-BASH-TAB" name="SHELL-BASH-TAB">8.3.2 Tab
Completion</a></h2>
<p>(Here comes something cool again.)</p>
<ol type="1">
<li>
<p>A commandline interface means lots of typing.</p>
</li>
<li>
<p>Typing is work.</p>
</li>
<li>
<p>Nobody likes work.</p>
</li>
</ol>
<p>From 3 and 2, we can determine that (4) nobody likes typing. Fortunately, <tt
class="COMMAND">bash</tt> saves us from (5) (nobody likes a commandline interface).</p>
<p>How does <tt class="COMMAND">bash</tt> accomplish this wonderful feat, you ask? In
addition to the wildcard expansion we discussed before, <tt class="COMMAND">bash</tt>
features tab completion.</p>
<p>Tab completion works something like this: You're typing the name of a file. Maybe it's
in your <tt class="ENVAR">PATH</tt>, maybe you're typing it out explicitly. All you have
to do is type enough of the filename to uniquely identify it. Then hit the tab key. <tt
class="COMMAND">bash</tt> will figure out what you want and finish typing it for you!</p>
<p>Example time. <tt class="FILENAME">/usr/src</tt> contains two subdirectories: <tt
class="FILENAME">/usr/src/linux</tt> and <tt class="FILENAME">/usr/src/sendmail</tt>. I
want to see what's in <tt class="FILENAME">/usr/src/linux</tt>. So I just type <tt
class="COMMAND">ls /usr/src/l</tt>, hit the <kbd class="USERINPUT">TAB</kbd> key, and <tt
class="COMMAND">bash</tt> gives me <tt class="COMMAND">ls /usr/src/linux</tt>.</p>
<p>Now, suppose there are two directories <tt class="FILENAME">/usr/src/linux</tt> and
<tt class="FILENAME">/usr/src/linux-old</tt>; If I type <tt
class="FILENAME">/usr/src/l</tt> and hit <kbd class="USERINPUT">TAB</kbd>, <tt
class="COMMAND">bash</tt> will fill in as much as it can, and I'll get <tt
class="FILENAME">/usr/src/linux</tt>. I can stop there, or I can hit <kbd
class="USERINPUT">TAB</kbd> again, and <tt class="COMMAND">bash</tt> will show a list of
directories that match what I've typed so far.</p>
<p>Hence, less typing (and hence, people can like commandline interfaces). I told you it
was cool.</p>
</div>
</div>
<div class="NAVFOOTER">
<hr align="LEFT" width="100%" />
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="33%" align="left" valign="top"><a href="shell-command-line.html"
accesskey="P">Prev</a></td>
<td width="34%" align="center" valign="top"><a href="index.html"
accesskey="H">Home</a></td>
<td width="33%" align="right" valign="top"><a href="shell-vt.html"
accesskey="N">Next</a></td>
</tr>
<tr>
<td width="33%" align="left" valign="top">The Command Line</td>
<td width="34%" align="center" valign="top"><a href="shell.html"
accesskey="U">Up</a></td>
<td width="33%" align="right" valign="top">Virtual Terminals</td>
</tr>
</table>
</div>
</body>
</html>