Graphical user interfaces are all around us. They've taken over the way we interact with our phones and our computers.
But not every computer has a monitor for interaction. In fact, the majority of computers on the planet don't have a monitor.
Instead, they're accessed using something called "the terminal"". In this way, millions of computers are manipulated and told what to do, without needing a physical monitor attached to them.
The terminal is a text based way to interact with the files and programs on your computer.
A terminal allows you full access to all the inner workings of your computer, through text based input.
Using a program called
ssh from within the terminal, we can change the computer we're working on to be a remote computer anywhere else in the world. The terminal then behaves with the same commands as if we were using our local computer.
The terminal is installed on almost every major operating system now, although today we'll be focusing on the one installed in MacOS / Linux.
The ability to work effectively with the terminal adds a key skill for the maturing developer. It gives you the ability to work with files in bulk, and to script and control a computer remotely.
If you're interested in server or web application development, having a decent understanding of the command line will give you the ability to deploy and manage your servers remotely. This also gives you an upper hand when it comes time to debug why systems stopped working.
On MacOS, the terminal can be opened by pressing
CMD+SPACE together, and then typing out
terminal. The MacOS launcher will open a terminal, which gives you a blank empty window, ready to receive text.
From here, you can start typing commands, and have them execute in a purely text based environment.
The first command you'll want to enter from within your terminal is
ls. That's the letter 'L', in lower case, followed by 'S', also in lower case.
ls command by pressing
This lists the files in the current directory.
In MacOS, you can type
open into the terminal, and you'll get a finder, showing exactly the files in your current directory.
If we use the command
cd, we can change into other directories. We type in
cd, and then the start of the name of the directory we'd like to switch into. If we press the
TAB key, the terminal will try to finish the name of the directory we've started typing.
There are a few special directory names in the terminal. One of these is
., which means the current directory. This is a way of specifying the place where you are currently.
The other special directory name is
.. (two periods). It means the directory above me.
Directory names can be chained together, using the
/ to link them. So if we wanted to go up two directories, we'd type in '../../'. And typing in
cd ../../ would bring us up two directories from where we currently are.
With this, you can see that the text based environment of the terminal allows you to move through the files and directories of your host computer.
Directories can be created with the memorable
To use it, just type out
mkdir directoryname. This will create a new directory, inside of the current location.
Just like before, we can use the
.. separators to specify different places to create our directory. For example, to create a new directory in the one above our current directory, we'd enter
Files can be created in a few different ways. Our programs we execute can create files for us, or we can use some built in utilities.
One of these is called
touch. Touch allows us to create new files on the computer, which are completely empty.
The way we use it is just by passing
touch a filename. It works just like
mkdir, in that you can pass a file path in the current directory or in another.
You can type out your own filename extension for the files you create with
touch. So, to create an empty Python file in the current directory, you'd only need to type out
touch myProgram.py, and
myProgram.py would be created in your current directory.
Editing text files is easy enough, using the
nano is run, it takes over the screen for the terminal. It then allows you to type directly into the file you've created in plain text. From here, you can also open other text files, and save them.
To open our
myProgram.py file we created before, we'd need to type in
nano myProgram.py. We could type out just
myP, and then press
TAB, and the terminal would finish typing the rest of the name of our file for us.
Once we've got our editor open, try typing out the following Python 3 program:
The way you save files in
nano is by pressing
CTRL+o. You can exit
nano, and go back to the terminal by pressing
You can then run your new Python program with
Nano is nice, in that it has a little bar at the bottom of the screen to let you know how to use it. That helper can be useful when you're just beginning your work in the terminal, and don't yet have all the shortcuts memorized.
Executing files is easy enough.
We just type out the name of the program we want to run, and hit enter.
If we want to find out where the programs we're running exist, we use the command
Which spits back out the directory where the program is being run from. Find out where your computer's Python3 interpreter is by pressing
In my case, it's
/usr/bin/python3. If I open up
myProgram.py, and change it to be the following:
I now have a program that the terminal can run on it's own. It will execute
/usr/bin/python3 myProgram.py, when I send it this filename.
But in order to let the terminal know that it can run this as a program, I'll need to do one more thing, and tell it that this file is now an executable file.
The way we do that is with the
chmod command. We type in
chmod +x myProgram.py, and the
chmod program turns it into an executable file on the system.
From here, we can run our program with a
With our new command, we're sending text to the terminal.
But one of the main features of the terminal is the ability to pass the output of programs into each other.
Let's try passing our program's output to the
less program, to see how output passing works:
$ ./myProgram.py | less
| or pipe operator is made by holding down
SHIFT and pressing the
\ key on the US keyboard. When we run the previous command, it takes over the screen, and we can see the results of our
less, just press
We can also write our program's output to a file. Try using the
> to create a new file like this:
$ ./myProgram.py > newFile.txt
You can then see the results of our program are written out to our new text file.
If you're interested in learning software development, or you know somebody who is, I've written a book called Make Art with Python, and it will be available for purchase soon. It teaches the fundamentals of programming through art, instead of bland print statements.
For now, you can sign up as a user, and get access to the first three chapters free, along with a video walk through for each chapter.