Introduction

A Brief Overview

We communicate with modern computers by clicking, tapping, sliding, hovering, typing, shaking, and speaking. Through simple gestures, we are able to command the computer. Many computer operating systems such as Windows and OS X provide a graphical user interface that enables us to open and view files, play games, turn on music, etc., all by simple movements and clicks of the mouse. Most operating systems that run on servers, desktops, and laptops provide another way of commanding your computer that many people have never even heard of. It is aptly called the command line interface (or CLI), the command line, or the command prompt.

Like a graphical user interface, the command line is one way that the operating system represents the computer's files, directories, and programs (which are also files) to the user. In fact, the command line is a text-based interface through which one can navigate, create, execute, and act on a computer's files and directories with precision.

What We'll Cover in this Book

Since the Linux operating system is the most commonly used operating system for servers, we will cover its command line interface in this book. Other operating systems such as Mac OS X and Unix are very similar, so you'll be able to use everything you learn in this book on those operating systems. Other operating systems, such as Windows, have command prompts, but they differ in some significant ways that make it difficult to cover their usage in one book.

This book mostly assumes that you're using the Bash shell, which is the default shell on many operating systems. Recent versions of macOS on Macs use a somewhat different shell called Zsh. For the most part, you won't run into too many of the differences during the Launch School curriculum. However, one difference is obvious from the start: the prompt displayed when your command line is waiting for a command. Bash displays a $ prompt by default:

$

Zsh, however, displays a % prompt by default:

%

Both Bash and Zsh let you change the prompt, so your actual prompt may be different. Furthermore, both shells change the prompt character when you're logged in as the super-powerful admin user, root. Under Bash and Zsh, the root prompt is #.

Since we're using Bash in this book and the rest of the Core Curriculum, we usually show the $ at the beginning of each command you need to enter. For instance, if you want to determine whether you're using the Bash or Zsh shell, you can run this command:

$ echo $SHELL
/usr/bin/bash

You should not type the $ at the beginning of that line. It is present only to show the shell's prompt. Instead, you should type echo $SHELL, then press the Enter key.

We may sometimes omit the $ if we're presenting a series of one or more commands that don't output anything to the terminal:

echo "First line" > file.txt
echo "Second line" >> file.txt
rm file.txt

However, we don't do this all the time.

From time to time, we do need to distinguish between Bash and Zsh, particularly with reference to the startup files: .bash_profile, .bashrc, .zprofile, and .zshrc. We will mention these as needed.

How to Use the Command Line

Using the command line is as simple as opening up Terminal (available on Mac and Linux) or iTerm (available on Mac only) on your computer, typing a command and pressing enter. Most commands will return some kind of output. Go ahead and open up Terminal and try typing the following command:

echo "Hello World"

You should see something like this:

$ echo "Hello World"
Hello World
$

There's nothing spectacular there, but the above exercise illustrates how you can command the computer to do something and then see its result.

What's in it for me?

While it may seem difficult at first, being able to use the command line will empower you as a computer user. Once you've learned the basics of using the CLI, you'll be able to simplify and speed up many tasks that were previously tedious. You can be very precise about how you want your computer to perform certain tasks, or about which pieces of information it should display. By reading this book and doing its exercises, you'll gain the following knowledge and skills:

  • Understand commands that tutorials tell you to use
    • As you learn programming, you will inevitably have to use the command line to install software, compile or run code, and perform other types of system administration.
    • Learning the basics of the command line will help you understand why you are running the commands, and give you a general idea of how they work.
  • Power over the computer
    • Being able to communicate with your computer or server via command line gives you immense and precise power over your computer.
    • With the command line, you can easily monitor your computer and its resources.
    • You can take advantage of a lot of powerful tools in their raw, powerful form.
    • You can learn to diagnose issues with your own software and others' software.