- Part 1 – First Steps in Terminal
- Part 2 – Navigating the File System
- Part 3 – Special Characters
- Part 4 – Commands
- Part 5 – Managing Files
- Part 6 – Symbolic Links
If you like this series and want to learn Terminal and the shell on macOS in more detail, get my book “macOS Terminal and Shell“
Terminal Application
On macOS you can find the Terminal application in /Applications/Utilities/
. There is a shortcut in Finder’s Go menu to get to the Utilities folder or use the keystroke ⌘⇧U. Terminal has a distinct black icon with a white prompt.
However, since you are reading this book, you are planning to use Terminal regularly. In this case, you really want to add the Terminal application to your Dock.
First Prompt
Once you have opened Terminal, it will show you a new window, which is white with black text by default. A short message will show the last login and then a line with the default prompt:
Mac:~ armin$
At first it shows the computer name (as set in the ‘Sharing’ pane in System Preferences. After the colon ‘:’ it shows a tilde ‘~
’. Then your user short name, followed by a dollar sign ‘$
’. Finally a dark gray block, the cursor, waiting for your input.
Note: if you have used Terminal before, you may have changed the default look of the Terminal and prompt. The functionality remains the same, no matter how it looks.
You can enter your first command. Type the letters p
, w
and d
and then hit the return key ↩︎
Mac:~ armin$ pwd
/Users/armin
You have typed the pwd
command and executed it with the return key ↩. The return key confirms a command and runs it.
When you mistype a command you can delete the last character with the delete or backspace key ←. You can also move the cursor through the characters you entered with the left and right arrow keys.
When you hit return with a wrong command or a command with a typo the shell will complain that it cannot find the command:
Mac:~ armin$ pws
-bash: pws: command not found
Terminal Terminology
We have already encountered a few different terms which may be very confusing in the beginning. The bad news is there are more terms, the good news is that it will all make sense eventually.
All of these terms date back to the days when computing started with big mainframe computers. Since computers were scarce, complex and expensive, many users had to share access. They did this by typing and reading on a ‘terminal’ or ‘console’ — a dedicated device talking to the main frame. Terminals and consoles may have looked somewhat like like modern workstations and computers, but did not have their own CPU. They would just allow to enter and send commands and receive and display the results.
Very early in the history of computing, terminals used mechanical printers or typewriters to show the results, these were called tele-typewriters or tty. The protocol that the mainframe used to communicate with the typewriters, was named tty
as well. The protocol and its name have remained, long after the mechanical typewriters are gone. Later the mechanical interfaces where replaced by terminals with electric keyboards and cathode ray screens.
As computers got smaller, more powerful and cheaper it became possible to have one on everybody’s desk. Even so, it was (and is) still convenient to run a shell, either locally or to connect to larger mainframes (servers). To do that you would open a program (or application) that ran a ‘virtual terminal.’
The Terminal application on macOS is such a virtual terminal program. To confuse things a little, macOS also has an application called Console. The Console application on macOS is not a virtual terminal or virtual console, but used to display and filter log files.
The terminal (whether virtual or real) only provides a means (virtual or mechanical) to enter and display text. It will display a ‘prompt’ which tells the user, that the system is ready and the user can enter a command, and a ‘cursor’ which shows where typed text will be displayed or inserted.
Shells
There is another program which interprets the text, executes code and sends the output generated back to the terminal.
This program (or class of programs) is called a ‘shell.’ A shell protects the user from the dangerous, complicated parts of the system and abstracts differences from one system to the other. Another way of looking at it, is that a shell protects the vulnerable, fragile parts of the the system from the user.
There are many different shell programs. One of the earliest shell programs was call just ‘shell’ or sh
for short. Surprisingly, sh
is still around after nearly fifty years.
On macOS the default shell is called bash
for ‘bourne again shell.’ This comes from the fact that it was developed as a replacement for bsh
or ‘Bourne shell,’ which was named after its main developer Stephen Bourne.
You will notice that unix shells and commands often exhibit a particular style of pun humor. ‘bash’ is merely the beginning.
Today, there are many different shells. Different shells have different ways of interpreting commands. The choice of shell is a personal preference and can be (as many things in computing) the cause of passionate argument.
Note: As system administrators, the choice of shell is not just determined by preference, but also practicality. There are a set of pre-installed shells on macOS and while it is possible to install additional shells, it increases complexity and management effort. Also when you are sharing commands and scripts with fellow administrators,
bash
is the commonly agreed upon shell.
bash
is available for most platforms and operating systems and is the default shell on many systems. Because of this prevalence bash is a good choice for your first shell.
bash has been the default shell for Mac since Mac OS X 10.3 (Panther). It is also the default shell on most Linux distributions and was the choice for the Unix command shell on Windows 10.
Note: the
bash
that ships with macOS is version 3.2.x. There is a newer major version:bash4
. However, sincebash4
is licensed as GPLv3, Apple still only includes the older bash 3.2. You can download and installbash4
if you want to, but many system administrators stick with the pre-installed version. We will be covering bash 3.2 here.
If you are curious, you can list all available shells on macOS with the following command in Terminal:
$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Warning: When entering commands, you have to watch that you type the command exactly as given, including spaces and other special characters. Terminal and shells are a ‘pro’ tool and assume that you know what you are doing. They are not forgiving to errors.
There are a few mechanisms that will usually protect you from ruining your system and data (and thus your day) entirely, but you need to always be careful and check.
Note: As we saw earlier the default prompt shows more information. However, this information (computer name, user name) is different for every Mac and user.
To keep things simple and short, when displaying terminal input and output, we will not show the entire prompt, but only the final character ‘$
’. This designates the commands that you should enter in your terminal, without the ‘$
’.
Subsequent lines without the leading ‘$
’ show output that you should expect from this command. Sometimes the output in this book will be abbreviated to make it fit the layout.
Technically, ‘graphical user interfaces’ (GUI) which display files, folders and programs as icons in windows such as macOS, Windows, KDE, Gnome and even iOS and Android are also shells, since they shield the system internals from the user and vice versa.
However, usually the term ‘shell’ is used for interfaces where the user has to type commands, or ‘command line interfaces’ (CLI).
Shell Scripts
Command Line shells commonly have two major roles. The first is to interpret and execute commands entered in an interactive prompt and deliver the results back to the user. The second role is to interpret and process list of commands, called scripts.
While scripts basically use the same set of commands as the interactive shell, scripts can also use control statements, loops, and variables which makes them a related task, but much more complex.
In this book we will focus on the interactive part of bash.
Next Post: Navigating the File System