The command line
Core Module
Contrary to popular belief, the command line (also commonly known as the terminal) is not a mythical being that has existed since the dawn of time. Instead, it was created at a time when it was not given that your computer had a graphical interface that you could interact with. Think of it as a text interface to your computer.
The terminal is a well-known concept to users of Linux; however, MAC and (especially) Windows users often do not need it and therefore encounter it. Having a basic understanding of how to use a command line can help improve your workflow. The reason that the command line is an important tool to get to know is that doing any kind of MLOps will require us to be able to interact with many different tools, many of which do not have a graphical interface. Additionally, when we get to working in the cloud later in the course, you will be forced to interact with the command line.
Note if you already are a terminal wizard then feel free to skip the exercises below. They are very elementary.
The anatomy of the command line
Regardless of the operating system, all command lines look more or less the same:
As already stated, it is essentially just a big text interface to interact with your computer. As the image illustrates, when trying to execute a command, there are several parts to it:
- The prompt is the part where you type your commands. It usually contains the name of the current directory you
are in, followed by some kind of sign:
$
,>
,:
are the usual ones. It can also contain other information, such as in the case of the above image which also shows the currentconda
environment. - The command is the actual command you want to execute. For example,
ls
orcd
. - The options are additional arguments that you can pass to the command. For example,
ls -l
orcd ..
. - The arguments are the actual arguments that you pass to the command. For example,
ls -l figures
orcd ..
.
The core difference between options and arguments is that options are optional, while arguments are not.
❔ Exercises
We have put a cheat sheet in the exercise files folder belonging to this session, that gives a quick overview of the different commands that can be executed in the command line.
Windows users
We highly recommend that you install Windows Subsystem for Linux (WSL). This will install a full Linux system on your Windows machine. Please follow this guide. Remember to run commands from an elevated (as administrator) Windows Command Prompt. You can in general complete all exercises in the course from a normal Windows Command Prompt, but some are easier to do if you run from WSL.
If you decide to run in WSL, you need to remember that you now have two different systems, and installing a package
on one system does not mean that it is installed on the other. For example, if you install pip
in WSL, you
need to install it again in Windows if you want to use it there.
If you decide to not run in WSL, please always work in a Windows Command Prompt and not Powershell.
-
Start by opening a terminal.
-
To navigate inside a terminal, we rely on the
cd
command andpwd
command. Make sure you know how to go back and forth in your file system. (1)- Your terminal should support tab-completion which can help finish commands for you!
-
The
ls
command is important when we want to know the content of a folder. Try to use the command, and also try it with the additional option-l
. What does it show? -
Make sure to familiarize yourself with the
which
,echo
,cat
,wget
,less
, andtop
commands. Also, familiarize yourself with the>
operator. You are probably going to use some of them throughout the course or in your future career. For Windows users, these commands may be named something else, e.g.,where
command on Windows corresponds towhich
. -
It is also significant that you know how to edit a file through the terminal. Most systems should have the
nano
editor installed; else, try to figure out which one is installed on your system.-
Type
nano
in the terminal. -
Write the following text in the script
-
Save the script and try to execute it.
-
Afterward, try to edit the file through the terminal (change
Hello world
to something else).
-
-
All terminals come with a programming language. The most common system is called
bash
, which can come in handy when being able to write simple programs in bash. For example, one case is that you want to execute multiple Python programs sequentially, which can be done through a bash script.Windows users
Bash is not part of Windows, so you need to run this part through WSL. If you did not install WSL, you can skip this part or as an alternative do the exercises in Powershell which is the native Windows scripting language (not recommended).
-
Write a bash script (in
nano
) and try executing it: -
Change the bash script to call the Python program you just wrote.
-
Try to Google how to write a simple for-loop that executes the Python script 10 times in a row.
-
-
A trick you may need throughout this course is setting environment variables. An environment variable is just a dynamically named value that may alter the way running processes behave on a computer. The syntax for setting an environment variable depends on your operating system:
-
Try to set an environment variable and print it out.
-
To use an environment variable in a Python program, you can use the
os.environ
function from theos
module. Write a Python program that prints out the environment variable you just set. -
If you have a collection of environment variables, these can be stored in a file called
.env
. The file is formatted as follows:To load the environment variables from the file, you can use the
python-dotenv
package. Install it withpip install python-dotenv
and then try to load the environment variables from the file and print them out.
-
🧠 Knowledge check
-
Here is one command from later in the course when we are going to work in the cloud
gcloud compute instances create-with-container instance-1 \ --container-image=gcr.io/<project-id>/gcp_vm_tester --zone=europe-west1-b
Identify the command, options, and arguments.
Solution
- The command is
gcloud compute instances create-with-container
. - The options are
--container-image=gcr.io/<project-id>/gcp_vm_tester
and--zone=europe-west1-b
. - The arguments are
instance-1
.
The tricky part of this example is that commands can have subcommands, which are also commands. In this case,
compute
is a subcommand togcloud
,instances
is a subcommand tocompute
, andcreate-with-container
is a subcommand toinstances
. - The command is
-
Two common arguments that nearly all commands have are the
-h
and-V
options. What does each of them do?Solution
The
-h
(or--help
) option prints the help message for the command, including subcommands and arguments. Try it out by executingpython -h
.
The-V
(or--version
) option prints the version of the installed program. Try it out by executingpython --version
.
This ends the module on the command line. If you are still not comfortable working with the command line, fear not as we are going to use it extensively throughout the course. If you want to spend additional time on this topic, we highly recommend that you watch this video on how to use the command line.
If you are interested in personalizing your command line, you can check out the starship project, which allows you to customize your command line with a lot of different options.