Bash Tutorial: Basics

Jeff Anderson java.emitter at gmail.com
Sun Feb 2 09:26:24 MST 2014


*Foreword*



Pardon me if this is super gumby and you already know all of this.  I'm
trying to start from the beginning at not gloss over too much.  Even at
that, I actually assume quite a bit here.  For those with a lot of
experience, I'm looking for feedback as to what I'm completely lying about.



*What is Bash?*



Bash is the "Bourne Again Shell".  It is the successor to the "Bourne
Shell".  AFAIK, it has nothing to do with The Bourne Identity series of
movies.  Nor is it a religious movement.



So what is a shell?  A shell is an enclosure that protects the thing inside
from the stuff outside.  In this case, you are the stuff outside trying to
destroy the inner workings of your computer.   By this definition, shells
have not been very successful.  You'll end up destroying your computer
despite its efforts to prevent this.  More specifically, a shell is a
program that lets you interactively issue commands to your computer.
Without a shell, you'd have to write and compile C/C++ programs that make
calls to system libraries whenever you wanted to do anything interesting on
the computer.  Basically, a computer system has these layers:

hardware

kernel and drivers

system libraries

utility programs and other apps

spyware, adware, and anything from Norton

shell

terminal

you



You can consider a graphical desktop environment as a visual shell.  Bash
is a text-based shell, that is a command line shell.  You type in text and
get text back as output, along with other side effects like deleting all
your files.  You can think of the C programming language as being an
abstract machine interface that sits between the hardware and the
kernel/drivers.  The last four layers can be collectively considered the
operating system (OS).  If you consider just the kernel to be the OS, call
it a software distribution.  The lines are a bit blurry to me.  Still, even
if none of the preceding is entirely true, it's a useful way to think about
it--for me at least.



*Why Should I Care About Bash?*



Unless you are or are trying to be a programmer, sysadmin, or something
similarly dysfunctional and anti-social, you probably won't care.  You
probably stopped reading once you hit the words "computer" or "definition"
and started reading my PowerPoint for Winners series.  You believe all
necessary computing problems have been solved via the popcorn button on
your microwave.



If not, a shell lets you do the same kinds of things you can do in a normal
desktop environment: create files, move files, delete files, search for
files, manage directories, create links/shortcuts, launch programs,
terminate programs, etc.  For certain activities, a command line shell like
Bash tends to be (far) more efficient and powerful than a visual
environment.  Also, Bash is the standard shell on Linux and is available on
Windows machines via Cygwin.  Since OS X is Unix-based, Bash is available
there too.  Basically, whatever environment you are working in, you'll
likely have the power of Bash available.  So, why not use it?



Another advantage of a textual shell, is that it is far more scriptable
than a visual shell.  The same commands that you type into the shell
interactively can be put in a file and used as a script.  This lets you
automate tedious things that would take you hours to do by hand.  Instead
of wasting your time on all that tedious stuff, you'll get to waste your
time on trying to figure out why your scripts aren't working!  I'll give
you a hint: you either have whitespace where it shouldn't be or don't have
it where it should be.  But seriously, hopefully your automation efforts
will produce a net gain.  In any case, it will be more fun, and more useful
to put on your resume than "double click" and "drag and drop" in the skills
section.



*How Do I Get Bash?*



This tutorial is written from a Cygwin/Windows 8 perspective.  Cygwin is a
Unix-like environment that runs on Windows.  If you install Cygwin, you
will have Bash.  It will be the default shell that runs when you run
Cygwin.  If you are on a Linux machine, you likely just have to open a
terminal program and you'll be running an interactive Bash shell.



So, download and install Cygwin.  I do an everything-and-the-kitchen-sink
installation.  This will likely take hours to complete, so be patient.  You
could do a full Ubuntu install on a VM faster.

http://cygwin.com/install.html



You should now have a shortcut on your Windows desktop called "Cygwin64
Terminal" or something similar.  Launch that, and you should now be in an
interactive Bash session.  A session is just a series of communications
between two or more entities that follows some sort of protocol
(communication rules).  So, you and the computer are now communicating by
the rules of Bash.  Be careful what you ask it to do, since it may well do
it.



*How Do I Know I Am Using Bash?*



I'll use a $ at the beginning of a sentence to indicate you should type a
command in at the shell prompt.  After the command, I'll list the expected
output.  This is a common convention.  So, do this:



$ echo $BASH_VERSION

4.1.11(2)-release



If you see something radically different, maybe your login shell is not set
to be Bash.



$ echo $SHELL

/bin/bash



$ cat /etc/passwd | grep $USER | cut -f 7 -d':'

/bin/bash



Don't worry if you do not understand the commands above.  If you are really
curious right now, read some man pages (man => manual).  Press 'q' to exit
back to the shell from reading the man pages.

$ man man

$ man echo

$ man cat

$ man grep

$ man cut



If your shell is not bash, you should be able to start bash anyway.  Ask
your system administrator to change your default shell to Bash if you have
to.



$ bash



*How Do I Escape from the Horrors of Bash?*



When you've taken enough abuse for one day, head for the safety of Windows.



$ exit



*What's Next?*



Read the Bash Guide for Beginners.  I intend to elaborate on topics in that
book as I read through it myself.



Here's some historical background on the command line that you may enjoy:
http://www.cryptonomicon.com/command.zip.



Patiently wait for me to write something else.  By then, you may well know
more than I do about Bash if you are a fast reader.


More information about the PLUG mailing list