old-www/HOWTO/From-PowerUp-To-Bash-Prompt...

106 lines
4.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>From Power Up To Bash Prompt: Bash</TITLE>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO-12.html" REL=next>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO-10.html" REL=previous>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc11" REL=contents>
</HEAD>
<BODY>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-12.html">Next</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-10.html">Previous</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc11">Contents</A>
<HR>
<H2><A NAME="s11">11. Bash</A></H2>
<P>If you give <CODE>login</CODE> a valid username and password combination, it will
check in <CODE>/etc/passwd</CODE> to see which shell to give you. In most cases on
a Linux system this will be <CODE>bash</CODE>. It is <CODE>bash</CODE>'s job to read
your commands and see that they are acted on. It is simultaneously a user
interface, and a programming language interpreter.
<P>
<P>As a user interface it reads your commands, and executes them itself if they
are ``internal'' commands like <CODE>cd</CODE>, or finds and executes a program if
they are ``external'' commands like <CODE>cp</CODE> or <CODE>startx</CODE>. It also
does groovy stuff like keeping a command history, and completing filenames.
<P>
<P>We have already seen <CODE>bash</CODE> in action as a programming language
interpreter. The scripts that <CODE>init</CODE> runs to start the system up are
usually shell scripts, and are executed by <CODE>bash</CODE>. Having a proper
programming language, along with the usual system utilities available at the
command line makes a very powerful combination, if you know what you are doing.
For example (smug mode on) I needed to apply a whole stack of ``patches'' to a
directory of source code the other day. I was able to do this with the
following single command:
<P>
<PRE>
for f in /home/greg/sh-utils-1.16*.patch; do patch -p0 &lt; $f; done;
</PRE>
<P>
<P>This looks at all the files in my home directory whose names start with
<CODE>sh-utils-1.16</CODE> and end with <CODE>.patch</CODE>. It then takes each of
these in turn, and sets the variable <CODE>f</CODE> to it and executes the commands
between <CODE>do</CODE> and <CODE>done</CODE>. In this case there were 11 patch files,
but there could just as easily have been 3000.
<P>
<H2><A NAME="ss11.1">11.1 Configuration</A>
</H2>
<P>The file <CODE>/etc/profile</CODE> controls the system-wide behaviour of bash. What
you put in here will affect everybody who uses bash on your system. It will do
things like add directories to the <CODE>PATH</CODE>, set your <CODE>MAIL</CODE>
directory variable.
<P>
<P>The default behaviour of the keyboard often leaves a lot to be desired. It is
actually readline that handles this. Readline is a separate package that
handles command line interfaces, providing the command history and filename
completion, as well as some advanced line editing features. It is compiled into
bash. By default, readline is configured using the file <CODE>.inputrc</CODE> in
your home directory. The bash variable INPUTRC can be used to override this for
bash. For example in Red Hat 6, <CODE>INPUTRC</CODE> is set to
<CODE>/etc/inputrc</CODE> in <CODE>/etc/profile</CODE>. This means that backspace,
delete, home and end keys work nicely for everyone.
<P>
<P>Once bash has read the system-wide configuration file, it looks for your
personal configuration file. It checks in your home directory for
<CODE>.bash_profile</CODE>, <CODE>.bash_login</CODE> and <CODE>.profile</CODE>. It runs the
first one of these it finds. If you want to change the way bash behaves for
you, without changing the way it works for others, do it here. For example,
many applications use environment variables to control how they work. I have
the variable <CODE>EDITOR</CODE> set to <CODE>vi</CODE> so that I can use vi in
Midnight Commander (an excellent console based file manager) instead of its
editor.
<P>
<P>
<H2><A NAME="ss11.2">11.2 Exercises</A>
</H2>
<P>The basics of bash are easy to learn. But don't stop there: there is an
incredible depth to it. Get into the habit of looking for better ways to do
things.
<P>
<P>Read shell scripts, look up stuff you don't understand.
<P>
<H2><A NAME="ss11.3">11.3 More Information</A>
</H2>
<P>
<UL>
<LI>There is a ``Bash Reference Manual'' with this, which is comprehensive, but heavy going. </LI>
<LI>There is an O'Rielly book on Bash, not sure if it's good.</LI>
<LI>I don't know of any good free up to date bash tutorials. If you do, please
email me a url.</LI>
<LI> source code, see
<A HREF="http://www.netspace.net.au/~gok/power2bash">Building a Minimal Linux System from Source Code</A>
for urls</LI>
</UL>
<P>
<P>
<HR>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-12.html">Next</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-10.html">Previous</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc11">Contents</A>
</BODY>
</HTML>