summaryrefslogtreecommitdiff
path: root/misc/slackbook/html/shell-bash.html
blob: 13ffdf08932006e89cbf74dfca38c1325daff2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!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>