Home »
Linux
Linux Operating System - CLI (Command Line Interface)
By IncludeHelp Last updated : October 19, 2024
Overview
If you are like most people, you are probably most familiar with using a Graphical User Interface (GUI) to control your computer. Introduced to the masses by Apple on the Macintosh computer and popularized by Microsoft, a GUI provides an easy, discoverable way to manage your system. Without a GUI, some tools for graphics and video would not be practical.
Prior to the popularity of the GUI, the Command Line Interface (CLI) was the preferred way to control a computer. The CLI relies solely on keyboard input. Everything you want the computer to do is relayed by typing commands rather than clicking on icons.
If you are new to a CLI, it is very tedious to memorizing commands and their options, but with time you can master it. However, a CLI provides more precise control, greater speed and the ability to easily automate tasks through scripting (see sidebar). Although Linux does have many GUI environments, there can be things which are not possible with GUI but with CLI everything is possible ( except graphical tools *wink*).
Command Line Interface (CLI)
The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer known as a terminal.
The terminal accepts what the user types and passes to a shell. The shell interprets what the user has typed into instructions that can be executed by the operating system. If the output is produced by the command, then this text is displayed in the terminal. If problems with the command are encountered, then an error message is displayed.
Accessing a Terminal
There are many ways to access a terminal window. Some systems will boot directly to a terminal. This is often the case with servers, as a Graphical User Interface (GUI) can be resource intensive and may not be needed to perform server-based operations.
A good example of a server that doesn't necessarily require a GUI is a web server. Web servers need to be as fast as possible and a GUI would just slow the system down.
On systems that boot to a GUI, comes with two ways to access a terminal, a GUI-based terminal, and a virtual terminal:
1) A GUI terminal is a program within the GUI environment that emulates a terminal window. GUI terminals can be accessed through the menu system. For example, on a CentOS machine, you could click on Applications on the menu bar, then System Tools → and, finally, Terminal:
2) A virtual terminal can be run at the same time as a GUI but requires the user to log in via the virtual terminal before they can execute commands (as they would before accessing the GUI interface). Most systems have multiple virtual terminals that can be accessed by pressing a combination of keys, for example, CTRL+ALT+F1.
Prompt in Command Line Interface (CLI)
A terminal window displays a prompt; the prompt appears when no commands are being run and when all command output has been printed to the screen. The prompt is designed to tell the user to enter a command.
The structure of the prompt may vary between distributions, but will typically contain information about the user and the system. Below is a common prompt structure:
The previous prompt provides the name of the user that is logged in (sysadmin), the name of the system (localhost) and the current directory (~). The ~ symbol is used as shorthand for the user's home directory (typically the home directory for the user is under the /homedirectory and named after the user account name, for example: /home/sysadmin).
Command Line Interface (CLI) - Shell
A shell is an interpreter that translates commands entered by a user into actions to be performed by the operating system. The most commonly used shell for Linux distributions is called the BASH shell. Shell provides many advanced features, such as command history, which can be re-execute previously executed commands.
The BASH shell also has other popular features:
- Scripting:
The ability to place commands in a file and execute the file, resulting in all of the commands being executed. This feature also has some programming features, such as conditional statements and the ability to create functions (AKA, subroutines).
- Aliases:
The ability to create short "nicknames" for longer commands.
- Variables:
Variables are used to store information for the BASH shell. These variables can be used to modify commands and features.