Bash Tutorial: Basics

Foreword

I wrote a few Bash Tutorial articles back in 2014.  I am now reposting them here.

It is very hard to write a very basic tutorial on anything having to do with software development.  Anyone that has developed software for years is steeped in a massive amount of background information that is impossible to completely unassume.  Now that I’ve washed my hands of all responsibility for failing to teach you anything (and presumptuously done so using the nonword “unassume” which I only assumed existed when I should have been more unassuming), I shall proceed.

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 lower-level programs in languages like C or C++ that make calls to system libraries whenever you wanted to do anything interesting on the computer.  That’s no fun, so we software developers have spend thousands upon thousands of man hours building layers upon layers of software to make it easier for you to talk to your computer.  And all you end up doing is posting a picture of your lunch on Facebook while watching a funny cat video on YouTube.  You’re welcome?  Anyway, a typical computer system has these layers:

hardware
dust
NSA-infected or Chinese-infected firmware
kernel and drivers
system libraries
Facebook, spyware, adware, and anything from Norton
shell
screen
the sticky notes comprising your enterprise project management system
your password, ‘password123’, written on the back of said sticky note
keyboard
dusty crumbs
a cat napping on the keyboard
mouse
you (at least the 15 minutes of the day when not on break, at lunch, or in a meeting)

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.

Why Should I Care About Bash?

Unless you are or are trying to be a programmer, sysadmin, or something similarly odd, 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 by Amazon Prime Pantry, the popcorn button on your microwave, and Netflix.

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 phenomenal cosmic 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.  Sure, you can use AutoHotkey (AHK) to do cool stuff on the desktop, but it’s more of a pain.  The same commands that you type into the shell interactively can be put directly 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.  I have seen people’s entire jobs replaced by relatively simple scripts.  You want to be the person writing the script, not getting replaced by it.  The robots are coming, my friends.  In any case, it will be more fun, and more useful to put “Bash scripting” on your resume than “google”, “double click”, and “drag and drop” in the skills section.  Some people have not sufficiently acquired the “google” skill yet though.  For sheer resume points, knowing Python is more valuable.  However, shell like Bash still shine in their interactive/tactical capabilities more than Python (which is a pure scripting language not intended to be used as an interactive shell).  You can script in Bash, but it is more of an awkward side note.  A very direct, portable, widely-used awkward side note, however.  Really, you want to know all three: Bash, Python, AHK.  There’s very little tactical programming that cannot be accomplished with those three.

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.  Contrast this with your children, coworkers, neighbors, pets, elected officials, and Amazon Echo.  Okay, I apologize Alexa–you sometimes do what I ask you to.

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.  I don’t actually understand them either, but that hasn’t stop me from writing this or finding gainful employment.  Just stay positive and exude confidence.  If enjoy having a basis for your confidence, read the man pages (man => manual) for the various commands.  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 (below).  Note that when I say ‘bash’ I mean the actual executing shell process on your computer.  When I say ‘Bash’, I mean the concept or language of Bash.  And when I say ‘bAsH’, it’s just a typo.  And when I put the comma outside the quotation marks, I do it intentionally.  Ask your system administrator to change your default shell to bash if desired.

$ bash

How Do I Escape from the Horrors of Bash?

You can’t.  It’s probably running on the server on every website you connect to.  Watching you.  Waiting.  Helping to serve up irrelevant ads for things you already bought.  If you’d like to try, once you’ve developed a case of carpal tunnel from all the other typing, type ‘exit’ to exit to the relative 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.  Or at least I intended to in 2014 before an endless barrage of other work derailed those plans.

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

Keep bashing away.  It takes years to become very proficient on the command line.  It is worth it if you intend to become a highly skilled software professional.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s