Don't Fear The Terminal!... A Quick Introduction To Bash

This blog entry is aimed at people who have pretty much no idea as to what the terminal is used for. It looks like an old-skool scary box and if you use it you might break the computer? That is what I also used to think too. But rest-assured: getting started with the terminal is actually pretty easy, and totally worthwhile :D

This is what you get when you open up the terminal on a Mac (found in applications/terminal):

terminal

You can change the window colour from the default of white to black by going to the 'Shell' menu and selecting 'Pro'... there are also lots of other colours if you want to change it up.

We can see that it defaults to Terminal--Bash. Bash is a UNIX shell and is the default shell on both Mac and Linux. Bash stands for 'Bourne again shell' as it replaced one of the first Linux shells named the Bourne shell. Windows has its own command-line (type 'Command Prompt') into the search bar in the Start menu to open up cmd.exe. You could also search for one called 'Power Shell'. There are also emulators of the Terminal such as Cygwin available for download. Some of the commands are the same but some are slightly different, if you use Windows I would advise you to keep reading because it is all quite similar... just if a command doesn't work try and figure out what it was supposed to be using this: http://ss64.com/nt/

So what does it do? Why would you use it?

Firstly it helps to understand what a 'Shell' does. A shell is any software that interfaces with an operating system. You are probably familiar with the Finder (Mac) or the Explorer on Windows. Here it is:

Snip20140808_6

We use it to navigate through folders to find files we want to open and Applications we want to run.

The Finder is a GUI which stands for Graphical User Interface. We see a graphic of a blue folder shaped icon in the finder, we click it, it opens a folder. The Finder allows us to interface with the operating system thanks to us clicking around on pictures of things.

The Terminal is a CLI which stands for Command Line Interface. If we wanted to navigate around, instead of clicking on icons, we just need to type in a command.

I know what you are thinking, wouldn't it just be quicker to click on icons than learn a load of commands? The answer is no. I've found that for now there are a handful that I use all the time, and if I need to find a command and can't remember it off by heart then I can easily Google it. Remembering a handful of commands doesn't take long and once you do I find that navigating via the terminal is so much faster, and the GUI just seems kind of slow and clunky. So if you are starting to learn development then definitely take some time to learn them, all those seconds saved will add up over time... and life is short after all.

Where do I learn these commands?

Go through this free online book: http://cli.learncodethehardway.org/book/

Download and print this: http://cli.learncodethehardway.org/bash_cheat_sheet.pdf

Write them on cue cards and practice them.

I was going to do an example of how to create a folder and copy a file into it with a load of print screens, but to be honest you are probably better off just going through Zed's book and learning there (http://cli.learncodethehardway.org/book/).

If you are starting to write code, it is quite possible that you might be using Sublime Text. It is really useful to be able to open stuff in Sublime using the command-line. Here is how to set it up:

  1. Open the terminal
  2. Make sure you have Sublime saved in your applications folder and you have a ~/bin directory in your path
  3. Enter this command and run: ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
  4. Enter this command and run it: open ~/.bash_profile
  5. This will open up your bash profile in text edit.
  6. You can edit this file to customise how you interact with bash.
  7. Copy this command into your bash profile (opened in text edit): export EDITOR='subl -w'
  8. Hit save and close it.
  9. In the terminal navigate into the top level directory of your coding project this is done using the command cd followed by the file path: cd type/in/the/file/path
  10. Use the subl command to open stuff in Sublime. You should be in the top level directory of your project, so to open it all up enter the command: subl . (the fullstop means 'all')
  11. If you just want to open a particular file then type subl followed by the filename: subl filename.rb

I have found this saves a load of time. Also it allows you to open up hidden files (the ones which start with a fullstop .) which I don't think (unless i'm missing something) is possible using the GUI. To see if your directory has any hidden files enter the command ls -a this will list all files... including the hidden ones.

If you want to go on to developing in Ruby then you can't really avoid using the terminal, so the quicker you get familiar with it the better. It will allow you to do lots of cool stuff.

What do you actually need the terminal for in Ruby?

Well in Ruby usually you would write your scripts using a text editor in files with the extension .rb. You then use the terminal to run those scripts.

So for the simplest example... Hello World we would need to:

1. Install Ruby (Copy the commands they give you: https://www.ruby-lang.org/en/installation/ I originally used Homebrew, I needed to upgrade to OSX Mavericks in order for it to work though...

  1. Open the terminal
  2. Use cd to navigate to the place where you want to store your code.
  3. Make a directory: mkdir directory_name
  4. Navigate into the directory: cd directory_name
  5. Make a file: touch hello_world.rb
  6. Open the file (if you have sublime and the short cut): subl hello_world.rb
  7. Copy this into the hello_world.rb file in your text editor and save: puts "Hello world!"
  8. In the terminal run: ruby filename.rb to run your program. In this case ruby hello_world.rb
  9. In the terminal we see:

Snip20140808_4

You can also try out script ideas directly into the terminal using an incredibly useful interactive console called irb. Simply type irb into the terminal and it will load a prompt. You can then start typing Ruby code into there:

Snip20140808_5

Another useful tip: If you are navigating around typing in file paths then the command prompt can autocomplete the rest of the word if you press the tab key. So instead of having to write a long file name such as HappyBearSoftware I can just press H and tab and it fills the rest of the word in. I was very happy when I learnt this.

So there is a very quick intro to the terminal. I also use it all the time with Rails for running servers and for version control with git... so very useful!

I am hoping that people will have read this, will start playing around with the terminal (realising it's not actually scary) and will maybe have a go at writing and running some simple scripts in Ruby.