Intro to Programming: the Command Line (Part 1)

Before we install Python, it is important to learn the basics of how to use the command line. This article is part of our Intro to Programming series.

What is the Command Line?

The command line (aka Terminal or Command Prompt) refers to a type of program that comes preinstalled with Windows, Linux and Mac computers and allows you to execute commands, run programs and navigate through the folders on your computer. In this article series, I will often use the term terminal or command line because I use a Mac, but know that they refer to the same thing.

The command line is often featured in movies - you may have seen it - particularly when hackers get involved and are doing something exciting, like breaking into government databases. Which is not to say that you should break into government databases!

For a little bit of history, before computers had a mouse or a touch pad, they also didn't really have advanced graphical interfaces with windows and buttons that you could click on, so the command line was really the only way to interact with them. You typically did this by typing at a physical "terminal" (which mostly consisted of a monitor and a keyboard) and whatever commands you typed at that terminal were then sent to the "mainframe", which executed the commands and sent back a result.



The mainframe was the actual computer and back then it occupied a whole room, so sometimes the terminal and the mainframe were not even in the same building. Of course, technology has evolved, but all operating systems to this day still have a built-in command line and even though today we have advanced user interfaces, the command line remains a powerful and, often times, faster way to interact with computers. In some cases, it is the best or even only way to achieve certain tasks, so it is worth getting comfortable with it. Advanced ways of using it are beyond the scope of this article series, but we will cover the basics.

On Mac computers, the command line is called a Terminal and you can find it by searching for the Terminal app. On Windows computers, the command line is called the Command Prompt, and again you can find it by searching for it on your computer. Linux users prefer the term "command line".

Note for Windows Users: The Windows command prompt is not quite as powerful (in my opinion) or as easy to use as a Linux or Mac Terminal. For this reason, I strongly suggest that you install the Anaconda Distribution, which "is a free, easy-to-install package manager, environment manager and Python distribution with a collection of 1,000+ open source packages with free community support." The Anaconda Distribution comes with the Anaconda Prompt, which is similar to a Linux or Mac Terminal. You can use that instead of the default Windows Command Prompt.

 

Whether you run the Terminal on a Mac, or the Anaconda Prompt, or even the Command Prompt on a Windows computer, a new window should open up and it should look more or less like this: essentially some text inside a plain box.

Shown above, is a screenshot of my Terminal window. Depending on your operating system version and other configurations, the font, text and colors may be different, but the functionality will be the same. The key difference between the command line and other software you may have used is that, well, it has no buttons, so the only way to interact with it is by typing commands and executing them.

For a mental model of the command line, think of it as a type of messaging app: you type some message and the computer types back some replies. You can't go above and change the messages you typed, but you can always type new messages. Also, unlike most messaging apps out there, you generally can't "type" while the computer is "typing". You need to wait for the command to finish running before you can run another command.

The command line remains a powerful and, often times, faster way to interact with computers.

 

It's ugly and I don't like it. Why should I use it?

I must agree, the aesthetics of the command line have not changed much since the 20th century. However, that's also part of its beauty: think about how much time you've spent getting yourself familiar with some graphical interface or another. Every application's buttons look slightly different, or are located in a slightly different location. The menu names are different, etc. The command line is simple because it's all text. If you can type on a keyboard, you can use a command line.

So despite the fact that it may not look welcoming, once you get a bit familiar with it, you will discover that the command line is actually easier and faster to use than some user interfaces. For example, in the screenshot above, you can see that all I need to do in order to get the current time and date is type the command  date and once I hit Enter I see the current date and time printed on the next line.

Another reason for learning to use the command line is the fact that sometimes, you just can't avoid using it. The command line allows you to interact with an operating system (OS X, Windows, Linux) in a much more intimate and efficient way, without graphical abstraction layers interfering. Also, a significant number of software that you will interact with as a programmer simply does not have a user interface (because it doesn't need to): web servers, daemon's of all sorts, command line utilities etc.

Anatomy of the command line

In the diagram below, I explain the various parts of the command line.

 

 

 

 

 

 

 

 

 

The command line prompt

The command line prompt is the text that gets prepended to every new line. In my case, it reads (base) ciprian ~$. This is highly customizable and it serves the purpose of providing useful information to the user. In my case, my command line informs me that I'm in the base environment (we'll talk about environments in an upcoming article), that the user I'm logged in as is ciprian (that's me), and that I'm in the ~ folder (more on this in just a bit). The $ at the end is just an indicator that this is where my command line prompt ends and this is where I can begin typing my commands.

Depending on your setup, you will likely see something very different on your computer. Customizing your command line prompt is a more advanced topic and we won't cover it here, but just be aware that it's something you can do.

The command to run

The command to run is always some sort of text. In the screenshot above, I'm interested in getting the current date, so the command I need to run is simply the word date. There are tons of commands you can run on the command line, but don't be overwhelmed by this. You can always learn new commands as you go. In the next article, we will cover the basic ones, to get you started.

The computer's response

This is simply what the computer prints as the result of running the command. Some commands print a lot of text on the screen, some print very little or none at all. It really depends on the command. You will notice that while the computer is running your command, you don't have access to the command prompt. If your computer is taking too long to respond, there are ways to force quit the execution of the command - we will cover this in the next article.

The mechanics of the command line, as I explained above, are pretty simple: you type your command, hit enter, wait for a bit (however long it takes the computer to run the command), and then observe the result. In my next article, I will go over some basic usage of the command line, which will come very helpful as we begin writing and running Python programs.

Read the next article in the series >

Read next

The Data Science Portfolio, Part Two: The Building Blocks

Let's take a deep dive into the ABC of building your first Data Science project.

Four Famous Female Coders You Should Know About

Women have been at the forefront of computing since its beginnings. Check out these four ladies whose work became game-changing.

Six Steps To Studying So It Sticks

Learning new things is hard. But it is also the best thing you can do for your happiness, health and productivity, at any age. Here are six strategies you can use to learn so it sticks.