Register to get access to 15+ free programming courses with interactive exercises

Command line interface CLI fundamentals

The more you use the command line, the more different programs you will encounter, many of which will become everyday tools. Despite the huge variety of such programs, the principles of their interaction with the user are largely the same. You can take any unfamiliar program in the GUI (Graphical User Interface), run it, read the names of the menu items, click the different buttons and get some results. In the CLI (Command Line Interface) you have nothing but the name of the program, and how you use it is unclear. For example, the program ls displays a list of files and directories. Here it's easy, just type it and press Enter.


Desktop Documents Downloads Library Movies Music Pictures Public

But what if we want to see hidden files and directories (on *nix systems, they start with a dot character, like .profile)? Then you need to type ls -a:

ls -a

.  .CFUserTextEncoding Desktop   Downloads Movies Pictures
.. .localized          Documents Library   Music  Public

What if we want to see the contents of the Public directory?

ls Public/

Drop Box

Now let's look at a more complicated example. Some programs can be confusing to use because of the way they are configured:

ffmpeg -i input.mp4 -vcodec libx264 -crf 30 output.mp4

The goal of this lesson is to see the system in use with console utilities. The good news is that there is one. The bad news is that not everyone clearly adheres to it.

Commands have arguments and options (also called flags). For example, in the command ls Music, Music is an argument, but in the command ls -a, -a is an option. Options always start with one or two hyphens. Another frequently used option to view the file list is -l, which displays additional information about each file.

ls -l

total 0
drwx------+  3 Guest  _guest   96 Nov 21  2017 Desktop
drwx------+  3 Guest  _guest   96 Nov 21  2017 Documents
drwx------+  3 Guest  _guest   96 Nov 21  2017 Downloads
drwx------+ 26 Guest  _guest  832 Nov 21  2017 Library
drwx------+  3 Guest  _guest   96 Nov 21  2017 Movies
drwx------+  3 Guest  _guest   96 Nov 21  2017 Music
drwx------+  3 Guest  _guest   96 Nov 21  2017 Pictures
drwxr-xr-x+  4 Guest  _guest  128 Nov 21  2017 Public

Options can be combined. To output all files, including hidden ones, with a detailed description, you need to type ls -a -l. Bash allows you to combine options and write ls -al even ls -la. Note that if you forget to put - before options and get the command ls la, then ls will assume that you want to see the contents of the directory la because it's now an argument. Using options in no way prevents you from using arguments (although this depends on the program). In the case of ls, it's possible to use both at the same time. To view the full contents of the Music directory with information about each file, just type ls -la Music:

ls -la Music

total 0
drwx------+  4 Guest  _guest   128 Nov 21  2017 .
drwxr-xr-x+ 89 Guest  _guest  2848 Aug 24 14:06 ..
-rw-r--r--   1 Guest  _guest     0 Nov 21  2017 .localized
drwxr-xr-x   9 Guest  _guest   288 Aug 26 17:25 iTunes

As you can see from the example above, options are entered to the left of the arguments, but sometimes they're entered to the right, this is more common in complex utilities with nested commands. We won't look at them right now. Sometimes it's hard to understand a notation like -tupa. Is this one option tupa or is it 4 options represented by one letter each, t, u, p and a, combined into one chain? In this situation, you need to see the documentation of the corresponding program. This is done with the man (manual) command. Just type man <command name> and it will let you read the documentation. The manual contains a lot of useful information, such as a description of the utility as a whole, how it's called, all its possible options, and even examples of calls.

Man Page

Try right now to see the manual of the ls program by typing man ls in the terminal. Press q to exit view mode, f (forward) to go forwards, b (backward) to go backwards.

Another useful site is, where you just need to type in any command and it will show a handy interactive description.

Explain Shell

Back to options. Most utilities have two versions of the same option, one long and one short. For example -v and --version in PHP.

php -v

PHP 7.2.7 (cli) (built: Jun 22 2018 06:27:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

php --version

PHP 7.2.7 (cli) (built: Jun 22 2018 06:27:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

Two options are made instead of one purely for convenience. While working in the terminal, it's important to be able to type quickly, so it's convenient to have short options. But when a developer writes a script consisting of different commands, it's preferable to use long options. It should be noted that long options are usually preceded by two hyphens, but some programs break this rule and use one, which can cause a certain amount of confusion. Long formats make it easier to understand what they mean. This is important so that the code is readable to other people and the developers themselves should they return to it after a long time. We won't touch on scripts yet, but you will see them for yourself in the future.

The options we looked at above have no parameters, but it's not uncommon to find options you need to do more with than just enter them. MacOS has a built-in say utility. If you pass it some text, it will say it (speak directly through the speakers). This program has the option --output-file (and its shorter version -o) which takes the path to the file. If entered, say will save a sound file on the specified path.

#  Instead of -o you can write --output-file
say -o hi.aac 'Hello, World.'

The option value is entered with a space after the option itself. If the option value contains special characters or spaces, it must be wrapped in quotes, either double or single, it doesn't matter.

say -o 'hi.aac' 'Hello, World.'

Some programs allow you to use the = sign instead of a space.

# The say command doesn't allow that, but you can see the principle
say -o=hi.aac 'Hello, World.'

In addition, say allows you to specify the input file to be read. If it is specified, say will ignore the text passed (as an argument).

# It also tells you which voice to read it in and where to record what was read
say -v Alex -o hi -f hello_world.txt

Now let's look at the documentation for say, namely the SYNOPSIS section. There we will see a description of how it can be called:

say [-v voice] [-r rate] [-o outfile [audio format options] | -n name:port | -a device] [-f file | string ...]

Almost every utility has a similar description. The descriptions are built on the same principle. Square brackets [] denote something that is not required. For example, the -v option is optional, and the same goes for any other options in this program. The vertical slash | denotes the operation "or", specifically the exclusive or. Look at the last block [-f file | string ...]. It means that say can either pronounce text from a file or pronounce a string passed as an argument, but not both at the same time. There are other variations in the descriptions of methods for calling, such as default value, selecting from specific elements, and negation.

And while we haven't covered everything you're going to encounter, let's stop here so your brain doesn't completely melt. Most importantly, don't worry if you don't feel like you've got it all memorized. Options require practice and experience, rather than memorization of theory. Now that you understand the general principles and you can look at the documentation, it's up to you to experiment.

Аватары экспертов Хекслета

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

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 the form, you agree to Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

From zero to a developer. Refunds in case you won't get a job

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending the form, you agree to Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.