The Anatomy of Commands
8 min readยทJan 1, 2025
A shell command is a structured textual directive entered into a command-line interface by a user to interact with an operating system or software application.
When entered, the shell processes the command, interprets its meaning, and executes the specified operations described by the command.
In some cases, the execution of a command may result in the display of textual data in the terminal referred to as the output of the command.
Each command follows a predefined syntax and semantics, and is usually composed of distinct elements, such as the command itself, flags to modify its behavior, and arguments that provide additional context or data for the command to act upon.
The command prompt
When opening a new terminal window, the first thing you should see appearing is a sequence of characters, usually terminated by a percentage sign % or a dollar sign $, called a command prompt.
razvan:~$
It is here to indicate to you that the shell is ready to accept commands, as it literally prompts you to take action.
Beside being a shell readiness indicator, the prompt can also be customized to include colors, special characters, and other elements, such as the current directory or the username, to make it more informative or just visually pleasing.
In this lesson and the following ones, the command prompt will be represented as a single dollar sign $.
$
The command syntax
A shell command is generally composed of:
- A command name.
- A list of optional flags.
- A list of optional arguments.
Each separated by at least one space character.
$ command flags arguments
The command name
The command name always comes first and is the core instruction that you want the system to perform.
Each command is associated with a program that is executed when the ENTER key is pressed.
For example, ls is the command name:
$ ls
The command flags
The command flags, also referred to as options, are used to modify the default behavior of a command.
They usually start with a hyphen - or double hyphen --, followed by a letter, a number, or a word.
For example, -l is a flag of the ls command:
$ ls -l
The command arguments
The command arguments, which typically follow the command name and any present command flags, are used to provide additional information or data required by the command to complete its task.
For example, -l is a flag and Documents is an argument of the ls command:
$ ls -l Documents
The command lifecycle
The lifecycle of a shell command refers to the series of steps that occur when a command is executed by the shell.
Here is a brief overview of these steps:
-
The command entry
The user types a command into the shell's command-line interface, then presses the
ENTERkey to submit the command for execution. -
The command parsing
The shell breaks down (or parses) the command entered by the user into individual elements to identify the command name, flags, and arguments.
-
The command execution
The shell searches for the program related to the command entered by the user and tries to execute it, or breaks the command lifecycle by displaying an error message on the terminal's output.
-
The command output
As the command executes, it may display output in the terminal window, such as data, messages, or results, allowing the user to witness the command's execution in real-time.
Note: Most of the time, the shell will not output any message in case of a successful command execution.
-
The shell prompt
Once the command has finished executing and its output has been displayed, the shell displays a new prompt, indicating that it's ready to accept another command from the user.
Running your first command
The echo command is a basic command used to display text in the terminal.
It takes as arguments a list of words called string of characters and displays them back in the terminal window, in the same order, separated by a single space character, followed by a new line.
A "Hello World" example
To display the famous "Hello World" string in your terminal, you can type the following command (without the dollar sign $) and press the ENTER key to execute it:
$ echo Hello World
As a result, you can see the "Hello World" words appearing right under the command line and the command prompt reappearing right under the output:
$ echo Hello World
Hello World
$
๐ Congratulations, you are now officially a shell user!
The importance of the command syntax
As mentioned previously, the command name, flags, and arguments must be declared in that specific order, as the shell may otherwise execute the specified command in an unintended way.
For example, the echo command has an optional -n flag used to suppress the trailing newline added to the output by default.
As you can see in this example, the command prompt reappears on the same line as the "Hello World" sentence, which shows that the -n flag effectively modified the behaviour of the echo command by suppressing the trailing newline:
$ echo -n Hello World
Hello World$
Whereas in this example, the -n flag is treated as an argument of the command and not an actual flag โ just like the words "Hello" and "World" โ and is printed back in the terminal followed by a trailing newline:
$ echo Hello World -n
Hello World -n
$
Note: Remember to always type the flags before the arguments, as you might otherwise end up with unexpected command behaviour.
Summary
Here's a summary of what you've learned in this lesson:
- The prompt is a character sequence indicating that the shell is ready to accept commands.
- Commands are composed of a name, optional flags, and optional arguments.
- Commands are executed by pressing on the
ENTERkey.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery