Register to get access to free programming courses with interactive exercises

Command Line CLI fundamentals

To work with the command line, you will need two things:

  1. Terminal
  2. Command shell

Terminal

Terminal

A terminal is a program that emulates the behavior of a physical terminal consisting of a keyboard and monitor. In the simplest case this is just a window with a command line inside. A good terminal is not limited to one window but allows you to open new terminals in tabs, just like tabs in a browser. This feature is available in the terminals that come with Ubuntu and macOS. The Windows terminal can't do much and requires replacement.

Some terminals allow you to make splits, that is, divide the window into parts. It's a handy mechanism, it's implemented, for example, in iterm2:

Split-screen in Iterm2

Good terminals also know how to recover a completed session, create profiles for different tasks, and much more.

Command shell

It has many synonyms, among them shell, command shell and command processor. A command shell is a program through which the user (or administrator) controls the operating system and installed programs using the command line. The shell differs from most other programs in that it is only a means to a certain task, not the task itself. The shell runs inside the terminal and invites you to enter commands.

~$ 

Each line sent to the system by the user is a command the system has to execute. After entering the command, be sure to press Enter, only then will it go to execution. Up to that point it can be edited.

The $ symbol is used as a separator. To the left of it is the configurable message to output, usually the current directory, and to the right is what the user enters. All examples in the future will be shown without this message and symbol. Below is an example of how to use the date command, which outputs the current date.

date

Sun Aug 26 14:02:59 CEST 2018
█

Terminal

Another example of a command is comments. As in any programming language, they don't affect anything, but we use them throughout our courses to describe what's going on:

# I am a comment

The command shell allows you to run installed programs, but that's not all it can do. For convenience, it's useful to have autocomplete for names of programs and files, and a history of entered commands, as well as the ability to navigate the file system, convenient hotkeys, and scripting support. Later in the course, we'll uncover many of these aspects. Command shells, like terminals, are different, although the default in most operating systems is Bash.

The command shell and the terminal are not the same thing. A terminal is a program that runs a shell inside itself.

Pipeline

As you will see later, the shell is a complete programming environment and many commands are standard constructs in many programming languages, such as variables, loops or conditions. Also, some commands are programs and some are not. In the coming lessons, I'll use the word "command" for simplicity, but later I will explain the difference

The command shell is often called a REPL (Read-Eval-Print-Loop), which is reflected in the way it interacts with the user:

  1. Read — the shell waits for the user to enter a command
  2. Eval — the shell executes the entered command
  3. Print — the shell outputs the result
  4. Loop — back to the first point

This process is called an interactive session. After loading, the shell waits for a command, then it executes it, displays the result, and then waits again for a command. This continues until a command is given to exit the terminal or shut down the computer. REPL is a widespread way to interact with the user. In the following courses, you will see that both databases and programming languages have this.

For your convenience, there are two terminals in the Hexlet environment that you can and should use while doing course exercises. The Terminals are available in the lower tabs (where the task description is) under the names Terminal 1 and Terminal 2.

It's easy enough to make mistakes while studying and experimenting. Always read the output carefully, and if you find any reason to worry, close and reopen the terminal. This will give you a fresh start (unless, of course, destructive actions were taken).


Do it yourself

In order to learn how to use the command line, you need to practice as much as possible. For this you will need a terminal.

  1. Install a terminal in your operating system if you don't have one. If you are using Linux or MacOS, your operating system already has a terminal installed. You can use it or download a more advanced version, such as iTerm2 for MacOS or Tilix for Linux. If you are running Windows, you will need to install WSL.

  2. Make sure that a bash session is running inside. You can do it this way:

    echo $SHELL;
    
    /bin/bash # - the output of the command entered above
    

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY