A 5 Minute Introduction to the Terminal
There's not much point in my reproducing here what you can find in buckets on the web, and there are links at the bottom of this article to good tutorials on how to use terminal applications. This article's purpose is to explain, I hope more convincingly than is generally the case, why you'd want to learn to use it in the first place, and why as a developer you are going to have to get to grips with it sooner or later anyway.
Plenty of articles on the web start with some propaganda about how fast and flexible and powerful it is to work on the terminal.
It's true, it really is.
The app in macOS is called Terminal, but the word terminal is also the general name given to programmes that provide you with a text based interface (as opposed to a graphical user interface, or GUI), with which you can read, write and run programmes and files anywhere on your computer (or, indeed, on networks it is connected to). That text based interface is the so-called command line, or command line interface (or CLI). A terminal may also be referred to as a console. So, you open a terminal to make use of the command line, but all three of these terms tend to get used interchangeably.
The programme that provides the command line interface is also often called a shell. The most popular shell, and the one that is default on macOS and most Linux systems, is called bash. There are others, which are similar, but my advice is to stick to bash until you have a good reason for changing. And don't get drawn into using tcsh at the moment, it's a different kettle of fish.
In the language of macOS, files are stored in folders. In the language of the command line, they are stored in directories. Same thing, different name. So you use the mkdir command to create a folder. Or a directory. Whatever.
What is a terminal?
The Terminal app provides you with a terminal (no surprises there), also known as a console. It's a screen full of text (or will be, once you get working on it). It's that rather intimidating screen that the geeks of Hollywood always use to hack into the alien space fleet thus saving the planet.
If you don't like the rather plain-looking default appearance of the macOS Terminal app, right-click on a terminal window and select Show Inspector. Select the Settings tab from the Inspector window, where you can choose something that looks more like it does on Mr. Robot or the Matrix.
A terminal window gives you direct access to the file system and the hundreds of programmes that are already installed as part of the Unix system that provides the bedrock of macOS. Most of these programmes are tiny, since they are nothing but text – no icons, no button images, just text.
Where do I find it?
If you use macOS, it is already installed in your Applications/Utilities folder. Hit command-space and start typing 'terminal' and let Spotlight do its autocomplete magic. Hit return and a new console will open up.
What does it look like?
A terminal window displays and accepts text. Nothing else. When a terminal window opens, you'll see a prompt that looks something like this:
My-Laptop-Name:someFolder myname $
It's not doing very much yet. It's just waiting for you to type in a command.
You're likely to see a tilde (~) the first time you open a terminal, which is a short form of writing your home directory (which would otherwise be displayed as /home/your_user_name/. This is the directory in the Finder that carries your user name. So instead of someFolder you'll see the tilde at first.
Often, you'll see that prompt abbreviated to $, which is the default prompt on many systems. Where you see this, bear in mind that you don't type in that $ yourself, it's just the prompt. So when you copy an example of CLI input like this;
$ mycommand --option
you'll type only this:
How is this easier/faster than using a mouse?
This is a really good question. At first, it seems totally implausible that typing commands into a terminal could ever be quicker than pointing a mouse and clicking. Especially when you factor in all those seemingly endless cryptic-looking commands.
Here are some reasons why it is, in fact, a much faster way to work, once you have a little practice under your belt:
- Using a mouse requires having the right windows open, but in the terminal, you can quickly access any location and any file from wherever you are in the folder hierarchy.
- Most users rely on a reduced set of commands in their daily use of the terminal, and most of what you need you'll use frequently. Remembering these few commands comes pretty quickly.
- Autocompletion is your friend – getting accustomed to using it can save you a LOT of typing. So do shortcuts, for example !! (to repeat the last command).
- It's pretty easy to write your own scripts and run them directly from the terminal.
Nobody types more slowly, or less accurately than I do, so if it's quicker for me, then you can be sure it's quicker for most people.
Do I really need to bother?
The short answer here is yes.
As a developer, it is very unlikely that you'll be able to do everything you need to without using the command line at least now and then. If Xcode is your main work tool, you'll find that it does many things for you, but by no means all. You'll almost certainly need to get up to speed with Git version control, and that means using the CLI. Xcode's support for Git was slow in coming and is still very meager.
Running local servers is no big deal, often just a single line of code, but that single line of code is typed into the terminal, and you'll need to navigate through the file system to do it. The easiest and quickest way to test an HTTP request is often the curl command, again, in the terminal. Many of the little daily maintenance tasls you perform will be done using the command line.
How long does it take to open a text programme, type in some text, and save it to a .txt file somewhere deeply nested in the file hierarchy? Not long, I'll admit. But compare that with how long it takes to type the following into a terminal:
$ echo meet mike after work >> Documents/mystuff/work/notes/today.txt
Okay, so how do I learn this quickly?
Just start today. Force yourself - open text documents with the terminal. Edit them in the terminal. If you need to copy some files, or a directory, or zip one, then do it in the terminal, at least during your development work. Don't bother testing little snippets in a Playground, just type swift into a terminal and use the REPL instead. Never use Calculator.app again – use Swift, or even Python, at the command line.
Once you notice that this is not just way cooler, but also faster, you'll probably need little encouragement to explore the command line interface more deeply.
Where to go next
Here are a few links that I hope you'll find both approachable and thorough.