3 Motivating factors

The command line is often seen in hacker movies, and seems like magic. After an initial overview of the command line features another often seen reaction is that it is an overkill. “Why would I need to understand how to work with this, just to make a folder, when I can do the same with the mouse in the File Explorer or Finder?” This is a legitimate question, but can be dispelled quickly when one realizes the enormous amount of tools available on the command line and their automation potential.

One of the most basic things you can do from the command-line is to launch applications. For example, let’s say we want to execute Python commands in interactive mode (aka REPL, read-eval-print loop). For this, all you need to do is execute the following command:

python

You’ll be greeted with the following prompt (notice how it changed to >>>):

Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is a good time to provide instructions on how to exit a running process. If you try the ESC key, normally this won’t work. The two common options are CTRL + C or CTRL + D.

Now you can play around with Python! Especially when you’re setting up Python with a virtual environment, you might want to check whether the command we just used runs the correct Python - the newly created one in the virtual environment, and not the system Python. For this we have the which command which will give you the PATH which is used:

which python

If you use a system Python, you’ll get something like /usr/bin/python3 as a result. We just mentioned the concept of the PATH, but what is that? The easiest definition is that this is an environmental variable providing the command line with the locations of executable applications. Environmental variables are very similar to variables in your programming language of choice, with the idea that they are always available (more similar to “global” variables). The function for printing things on the command line is echo. We can use this to have a look at the PATH variable. Note that variables in the shell are decorated with the $ symbol:

echo $PATH

This should output many lines to the console. If you read through them, you should be able to recognize some applications that you can run from it. A common thing that will happen when you install software is that you will need to manually append (add to) the $PATH variable the locations of your newly installed software. This is often done through configuration dotfiles, such as .bashrc and .zshrc.

After this theoretical section, let’s go back to practicals. A common use case for launching applications from the command-line is when you are working on a new project, stored in a new folder, and you don’t want to waste time navigating your file explorer, to open it with your IDE. Here you can do the following, for this example we assume you are using VSCode:

vscode .

Don’t miss the . symbol, we’ll explain it later!

The command-line is often used to install software. This is especially common for non-windows operating systems, and for software which is used for development or research purposes. For example, here are the typical commands used to install software on Linux and Mac OS:

# with apt-get
sudo apt-get install python

# With Homebrew
brew install python

Here we see a few key elements that we’ll keep seeing. First, sudo, which means “super user do” or “substitute user do,” basically let’s you run commands as the system administrator (aka sysadmin, or super user). Second, apt-get accesses a repository of packages and install asks to install the specific package listed, in this case the python programming language.

In the second command, we use Homebrew, a popular alternative to apt-get on MacOS and Linux, to accomplish the same task.

Install Homebrew using:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

We can test it by installing one fun package, called cowsay:

sudo apt-get install cowsay

This package has just one function. You can pass it a string, and it will return you a fun output:

cowsay hello
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Of course, not very useful, but illustrates the point!

If you are on windows, Chocolately is a package manager that works like apt-get. For example:

choco install git

Follow the installation instructions here.

Cmder is a command line emulator that allows you to use the same Unix commands that are common on mac and linus systems.