Bash Scripting 101

Have you ever wondered what it would be like to gain complete control of your Linux box?  If so, you really need to understand Bash and the wonderful world of Bash scripts.

Bash is a command line interpreter that is extremely ubiquitous.  Almost every major Linux distribution uses the Bash shell (including Macs) and you can use it to do all sorts of Linux foo like automating tasks.

The one catch is that Bash is a little different than regular programming languages so we’ll look at those nuances a little later.

In this tutorial, we’ll be using Xubuntu Linux Virtual Machine but you can use anything that runs Linux. Let’s jump right in.

First up, we need to make sure you have the latest version of Bash.

Kick open a Terminal window and type:

echo $BASH_VERSION

bash versions

As long as you’re on 4.3 or newer you’ll be fine for these tutorials.  If not, get with the program and get a newer Linux distro.

WTF is Bash anyway?

Bash is a command line interpreter, meaning, it’s just a program that understands commands.

You type it responds.

It was first released in 1971 as the Thompson Shell but then in 1977 Jason Bourne modernized the shell…

just kidding

His name was actually Steven Bourne but I couldn’t resist the Robert Ludlum allusion.

So in 1989, Bash was released as the Bourne Shell under the GNU project.  It game the Bourne Again Shell since it was reopened as an open source project spawned from Steven Bourne’s original Bourne Shell.

Essential Bash Commands

The first command that all Linux geeks know: pwd prints the working directory.

If you were standing in your house and yelled: “Woman! Make me some corn bread!” and your wife were there, she would smack you over the head right?  In that case, your pwd would be your house and any commands you yell would be yelled from home.

In the same way, when you type pwd you’re saying, “Yo BASH, show me which directory I’m currently standing in.”

This also means any commands you run will automatically run from the current directory.

pwd

If you want to look around type:

ls

ls

The green text represents files and the purple stuff are folders.  You colors will probably be different; heck, you might not even have any colors but don’t worry I’ll show you an easier way to distinguish files from folders later.

For now, just think of ls as a shorthand form of list.  ls is tantamount to typing dir in Windows.  You get to see the files and folders in directories.

To see hidden files too type

ls -la

ls -la

In Linux hidden files start with a dot (.) and are hidden from the normal ls command.  But if you add ls -la this means show me all items in the long format so I can see when they were created and show me hidden files too.  That’s that the “a” after that “l” in ls -la does.

For example, the first line of the above output show a file named “.” (dot).  This dot file is hidden by default but it is an alias for the current directory.  But that’s a little confusing to understand now.  Scroll down to the last line:

.xsession-errors

This is a hidden file named .xsession-errors.  Now how did I know it was a file and not a folder?  Because the first character of the last line starts with a (-) dash.  If it started with a “d” it would be a directory.

Here’s the full line:

-rw--------- 1 vonnie vonnie 190 Jan 9 21:55 .xsession-errors

Since the first character in the line output is a dash we know this is a file.  Again, if it started with a “d” then it would be a directory.

If you want to make a directory just type:

mkdir

and to delete a directory type

rmdir

If you want to copy a file in Windows you would just right click it and choose copy or press Ctrl+c.  Not so in Linux!

But thank God it’s not so bad.  Let’s say I wanted to copy the file passwords.log and I wanted to name the copy new_passwords.log.  All I would need to type is:

cp passwords.log new_passwords.log

And then to delete the new file (or any file) I can just remove it like so:

rm new_passwords.log

Viewing inside files

I used to have a pet cat in high school.  She was a stray cat, my family scooped her up when she was a kitten but she was wild.  We found her out in a field somewhere and claimed her as our own.  Very quickly she grew up, grew claws and started getting into everything she could find.

That’s how I remember the next command: cat.  It lets you “get into everything you can find”.  In other words, you can use cat to peek inside text files.  Windows users would double click a file to open it, Linux users can just use cat followed by the file name.

cat credit_cards.txt

cat

If the file is too long type:

less credit_cards.txt

and you can page through each screen with your spacebar.  Finally to peak at the first few lines type:

head credit_cards.txt

or the last few lines only type:

tail credit_cards.txt

Head is good when you know you don’t need to see everything in the file; perhaps you just want to see the begining comment of a script so you can see who created it.  Tail is good for when yo uwant to view the most recent log entries.

For example

tail /var/log/messages

Will show you the most resent event posted to your logs file, known as messages in Linux.  Also notice hte file has no extension, this is perfectly fine and used a lot in Linux so get used to that.  Files don’t need extensions as they do in Windows.

The Bottom Line

I’m not going to bash your brain with a bunch of bash commands so let’s keep it light for now.

In summary you learned:

  • BASH is a command line interpreter meaning it inerprets commdans typed in the temrinal window.  Think of it like the DOS prompt in Windows 98 or the Command Prompt in Windows XP and newer.
  • pwd prints the working directory path
  • ls lists files and folders.  ls -la shows hidden files too.
  • mkdir makes a new directory and rmdir removes and deletes that directory. rm deletes files.
  • cp milky way copies a file named milky into a new file named way.  Remember Linux doesn’t give a crap about extensions so have a file named milky with no .txt or .log extension is perfectly normal.
  • head dragon shows the beginning lines of a file named dragon, tail dragon shows the last few lines of a file named dragon and cat dog will print out the contents of the dog file.  If it’s a text file all is well; however, if dog were an image you would just a bunch of garblygook because cat doesn’t understand binary data like images.  Give it text and it purrs all day but feed it a binary and you’ll get a bunch of crap

Bam! Stay tuned for next time – we’re just getting started baby

About

Connect with Vonnie on Twitter

Posted in Code, How To, Linux Tagged with: