Skip to content
John Fraboni edited this page Dec 9, 2014 · 19 revisions

Table of Contents


##Code and Quiz

Code a little

Take the quiz


##What Is This Thing And How Do I Stop It?

###The Original Handshake

The command-line interface is a powerful text-only tool for interacting with computers, and until the advent of GUI (Graphical User Interface) based operating systems, like Windows or Mac OS, the command-line was the primary tool for controlling computers.

###A Program That Runs Programs

What is the command-line? The command-line itself is essentially a program, a command interpreter, that on our behalf, executes other programs, often called jobs. The executable program run by the command-line can be a utility or a server of some sort, or any number of applications.

An operation system is comprised of two main parts: The first main part is the kernel, responsible for allocation of machine resources to the system programs. System resources include like memory, disk space, and CPU cycles. The second part are the system programs, which includes drivers, applications, shells, and any other file.

Falling into the second main part of an operating system, the command-line belongs to the system programs.

###Nomenclature

People often use the terms terminal, shell, and sometimes even prompt to mean, the command-line. These terms are synonyms, so if you hear or read, go to the terminal, or, open a shell, that is to say, go to your terminal.

###Textual Interface

The command-line is a textual interface, meaning it only deals with text input and displays text output. It's as simple as that: no graphics. In fact, on all GUI operating systems, like Mac OS X or Window, the terminal is actually a terminal emulator, that is, a GUI application designed to look and act like the terminals that ran on older systems without graphics processors.

###The Prompt

On a Linux operating system, when you launch a terminal, you are often greeted by an issue message, identifying the version of Linux and perhaps the type of device you're using. You'll then be presented with a prompt, often comprised of some information, a prompt symbol, then a cursor at which you can type text to enter and execute commands.

The prompt symbol will often be a $ or # or sometimes a > character. The prompt character can be configured, so that's why you sometimes see a variation.

On Cloud9, the bash terminal will print:

  1. The name of the user with which you've logged in to the system
  2. Next, an @ symbol, then a domain name.
  3. Finally, following the username and domain name is the directory path in which you are currently situated.

The following image ends with even more information (master), which is the git branch in which we're working:

Code a little

##Bash

There are many types of command interpreters, and the one we'll be dealing with is bash running on a Unix-like system. Bash stands for Bourne Again Shell, a play on the fact that it is a superlative version of an older command interpreter named for and written by a guy named Bourne.

TIP: If you want to know which command interpreter you're running, type the following command at the prompt:

myuser@myproject:~/workspace (master) $ echo $0
bash

This will expand and return the name or path to the executable.

##The Executioner

###Executing Commands

We execute commands on the computer by inputting the name of the command, like ls or pwd, sometimes accompanied by one or more arguments, then pressing the Return/Enter key. The ls command stands for list services, and will print a list of all files and subdirectories within the current directory. The pwd command stands for _present working directory`, which prints the path to your current directory.

$ find / -name foo

In example above, the command is find, and we're passing in the first argument of /, which here means start the search from the root directory, then we're passing the -name flag with a value of foo, which specifies the name on which to perform the search.

###Arguments

Command-line arguments are values or information we pass to a command. The command can be written to either expect arguments or optionally handle them.

The list services command optionally takes a path to a directory for which you want to list all contents, services.

Running the command like so:

$ ls

...lists the services in the present directory. Running the command with an optional path as an argument, like so:

$ ls path/to/some/directory

...lists all of the services in the directory at path/to/some/directory.

###Flags

Commands can optionally handle flags. Denoted by the - prefix on a letter or word, flags can behave like switches: if passed, the switch is on, if not off, etc. Again, the ls command can be executed with the -a flag, which means, include any hidden files:

jfraboni@shoutout:~/workspace (master) $ ls -a
./   .bowerrc  .git/       README.md   config.xml   hooks/         package.json  plugins/  www/
../  .c9/      .gitignore  bower.json  gulpfile.js  ionic.project  platforms/    scss/

ls can be executed with the -l flag, meaning, show the long form, and will provide extra information on each file, such as permissions:

jfraboni@shoutout:~/workspace (master) $ ls -l
total 44
-rw-rw-r-- 1 ubuntu ubuntu  207 Dec  5 16:59 README.md
-rw-rw-r-- 1 ubuntu ubuntu  123 Dec  5 16:59 bower.json
-rw-rw-r-- 1 ubuntu ubuntu  804 Dec  5 16:59 config.xml
-rw-rw-r-- 1 ubuntu ubuntu 1353 Dec  5 16:59 gulpfile.js
drwxrwxr-x 3 ubuntu ubuntu 4096 Dec  5 16:59 hooks/
-rw-rw-r-- 1 ubuntu ubuntu   40 Dec  5 16:59 ionic.project
-rw-rw-r-- 1 ubuntu ubuntu  360 Dec  5 16:59 package.json
drwxrwxr-x 4 ubuntu ubuntu 4096 Dec  5 16:59 platforms/
drwxrwxr-x 5 ubuntu ubuntu 4096 Dec  5 16:59 plugins/
drwxrwxr-x 2 ubuntu ubuntu 4096 Dec  5 16:59 scss/
drwxrwxr-x 8 ubuntu ubuntu 4096 Dec  9 00:34 www/

Some flags can accept values, and as such, are like named arguments, like we saw with the find command:

$ find / -name foo

Here' -name takes the value foo.

Flags don't have to be in any particular order, and single letter flags can be compressed together, like -rf, which might mean, recursive and force.

###Command Not Found

If the command we're trying to execute is installed on the system, the command-line interpreter will find the application responsible for handling this command, execute it, passing to the application any arguments or flags we've provided. If the command is not install, the terminal will report by saying:

myuser@myproject:~/workspace (master) $ opsparkrocks
bash: opsparkrocks: command not found
myuser@myproject:~/workspace (master) $ 

TIP: Remember, to execute a command, you first type the command, then you MUST press the Return/Enter key - this gives you the chance to check your typing!

##Practical Stuff

###Fixing Mistakes

Nobody is a great typer, that's why it's not an Olympic sport - you'll make mistakes at the command-line often.

Getting use to correcting yourself quickly should be one of the first things you learn:

On a Mac, using the control modifier key, we have access to several shortcuts to improve our command-line input

Command Action
delete : Erase one character at a time
control-w : Erase one word at at time
control-u : Erase the whole line from the cursor to the prompt
control-d : Erase the next character forward
control-h : Erase the previous character forward, same as delete
control-k : Erase to the end of the line
control-y : Paste the last deleted character, word or line

##Cursor Movement

Command Action
left-arrow : Goes left, one character at a time
right-arrow : Goes right, one character at a time
control-a : Goes to the beginning of the line
control-e : Goes to the end of the line
control-xx : Toggle between start of line and current cursor position

##Killing a Process

We use the terms process, job, service, command and application interchangeably - they mean, the program that runs when we execute a command.

You'll often need to kill a service or process that is running, maybe because you've launched it prematurely, launched the wrong command, it's time to shutdown a server, or the process is taking too long.

To kill a process on Linux, we can ask the operating system to send our process the TERM signal by typing control-c. Here's a list of other handy kill type signals:

Command Action
control-c : Kills any running process
control-z : Suspends any program
fg : Foreground, will resume a suspending program

##Command History

You can see the history of the commands you've executed by using the up and down arrow keys. Doing so will cycle backwards and forwards through the history of your work, and this is handy for re-executing commands you'll use often, or, if you make a mistake and want to correct and run the command again.

Command Action
up-arrow : Shows the previous command executed
down-arrow : Shows the next command executed, if any
control-p : Shows the previous command executed
control-n : Shows the next command executed, if any

##Superpowers

The terminal still offers the most powerful way to interact with Unix-like Systems. As you become proficient as a developer, you will find yourself using the command-line more and more to assist in your work, so it's best to get use to it at the outset. In no time, you'll be controlling your computer like a pro.

While it may seem like clicking buttons or selecting menu options with your mouse is a more effective way of interacting with computers, for many reasons, the command-line is still offers more control.

Some reasons this is true are:

  1. Firstly, using a mouse slows you down - you have to take your hand off the keyboard, grab the mouse, and start looking for UI elements that match your needs, and this breaks your focus, taking you away from your active, creative and productive thought stream.

  2. Secondly, some commands available to you in a shell outstrip the features of the GUI equivalent.

  3. Finally, you can quickly write powerful shell scripts, programs that run in the shell interpreter, that can perform some dazzling computation.

##Become a Superuser

Learning to use the command-line effectively can seem daunting when you're new at it. All these phrases you type into this black window, some take input, how does anyone get good at this? I find the best way to learn is to have a reason to learn, and you'll certainly have that reason as you progress in learning to write apps.

Here's some places to start:

Code a little

Take the quiz


##Finding The Terminal

###Opening a Terminal On Cloud9

On Cloud9, the terminal is usually open by default, and located in a tab of the Console View, usually positioned at the bottom of the screen slightly to the right-side. The command-line tab will say "bash", followed by your username. Your Cloud9 color configuration may be different from these screenshots, but here's an image showing the bash terminal on Cloud9:

Code a little

If you cannot see the Console View, try selecting from the Cloud9 menu bar View > Console. Check out this screen shot:

Code a little

If the Console is open, but your command-line was closed (you can close tabs in the Console View by clicking the "x" on each tab, and sometimes this confuses newbies), select the "+" sign to the right of the last open tab on the Console, then select New Terminal. Here's another screenshot:

Code a little

###Opening a Terminal on Mac OS X

On Mac OS X, open spotlight with the keystrokes, Command-Space, then start typing "Terminal". The Terminal program will show up in the dialogue box and you can simply select it and hit return - this will launch the terminal.

We highly suggest installing iTerm2 - it's a great terminal program with more advanced features than the stock terminal that comes with OS X.

###Opening a Terminal on Windows

On Windows, press the Windows button, then type cmd into the prompt dialogue, then hit return, this will launch the terminal on Windows. The commands on Windows are often different from Unix-like systems, so keep that in mind. If commands we discuss are not found on Windows, you can Google for solutions to installing GNU equivalent for Windows. Even if you're working on a Windows machine, the exercise below is meant to be completed in Cloud9, which uses an Ubuntu operating system, which is a Linux distribution, and by default uses a bash terminal.


##More Learning Resources

One of the most powerful features of the Internet is that others have shared their knowledge, so instead of re-inventing the wheel on this one, I'd like to shout out to Zed A. Shaw, who took the time to write and share an excellent primer on, among other topics, learning the command-line, and so I highly recommend going through these lessons:

Command-line Lessons

© 2014-2015 Operation Spark

Clone this wiki locally