SearchWindowsServer.com

command-line interface (CLI)

By Peter Loshin

What is a command-line interface?

A command-line interface (CLI) is a text-based user interface (UI) used to run programs, manage computer files and interact with the computer. Command-line interfaces are also called command-line user interfaces, console user interfaces and character user interfaces. CLIs accept as input commands that are entered by keyboard; the commands invoked at the command prompt are then run by the computer.

Today, most vendors offer the graphical user interface (GUI) as the default for operating systems (OSes) such as Windows, Linux and macOS. Most current Unix-based systems offer both a command-line interface and a graphical user interface.

The MS-DOS operating system and the command shell in the Windows operating system are examples of command-line interfaces. In addition, programming language development platforms such as Python can support command-line interfaces.

The command line dropped in popularity following the introduction of GUI-based personal computer OSes like Microsoft Windows and Apple's "classic" Mac OS in the 1980s. The command line remains an important tool for IT professionals, software developers, sys admins, network administrators and many others who prefer a more precise and reproduceable interface to their systems.

What is a shell?

In computing, a shell program provides access to an operating system's components. The shell gives users (or other programs) a way to get "inside" the system to run programs or manage configurations. The shell defines the boundary between inside and outside.

The following are the two types of operating system shells:

Bash is the most commonly used command-line shell for Unix-based OSes including Linux.

The software that handles the command-line interface is commonly referred to as a command language interpreter, a command processor or command interpreter. Two well-known CLI shells are PowerShell for Windows and Bash for Linux and macOS.

Shells are the outermost layer of the OS and are often separated from the underlying OS kernel. A shell operates like an application and can be replaced as needed. An OS may have more than one shell available, as in the following examples:

Because the shell is only one layer above the OS, users can perform operations that are not available in other interface types, such as moving files within system folders and deleting locked files.

To get the greatest benefit from using a CLI shell, users should learn a scripting language. Most command line shells can save sequences of commands in a script or batch file which may be fully programmable. Shell scripting is the foundation of basic systems management automation.

How do CLIs work?

Once a computer system is running, its CLI opens on a blank screen with a command prompt and commands can be entered.

Types of CLI commands include the following:

CLI is more than a simple command/response system, as most have additional features that make one preferable to another. Some features include the following:

Commands and syntax for CLIs tend to be very stable over time, in part to enable backward compatibility for scripts.

Prompts and commands

Depending on the operating system and the CLI, there may be hundreds or even thousands of different executable commands available. The set of commands may vary dramatically between operating systems or applications.

For example, Microsoft defines hundreds of cmdlets as "lightweight commands" to be used in PowerShell; PowerShell functions are lists of cmdlets that can be run at the command line.

Some commands run at the command line by themselves, such as the dir command:

C:\> dir

Commands can also be used with arguments (sometimes referred to as parameters) that modify how the command should be run. Arguments follow the command and provide additional details or specifics.

The cd "change directory" command in many CLIs is used to change the current working directory when the desired new directory path is included as an argument -- but in some cases (as in MS-DOS or the Windows command prompt) the command by itself returns the current working directory.

This command resets the current working directory:

cd \documents\user\working

Arguments enable command-line users to get very specific results that are sometimes not available through a graphical interface.

Modern CLIs provide pipes, or mechanisms for taking the output of one command and sending it as input to another command. The vertical bar, or "|" symbol, is often used for this purpose.

The following "one-liner" uses pipes to string together several PowerShell commands to retrieve detailed network configuration information from a Windows machine:

Get-NetIPAddress | Sort InterfaceIndex | FT InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress

Even though this string is entered at the command line as if it were a single command, it includes the following consecutive commands:

Backward compatibility is often a goal when new CLIs are introduced to replace existing CLIs, as when Microsoft replaced MS-DOS with the Windows Command Prompt. Though some MS-DOS commands are no longer supported, most will still work in PowerShell as originally intended.

Common MS-DOS commands include the following:

Command Example Notes
CD C:\> CD \user\programs
C:\user\programs>
Changes the current directory to the specified path. When entered with no path, it displays the current working directory's name. If the path is included in the command prompt, the prompt will change as in the example (see also PROMPT, below).
CHKDSK C:\> CHKDSK a: Checks the specified disk -- in this case, the floppy disk in the A: drive -- and returns a status report showing the disk size, number of files and directories in use and number of bytes used.
COPY C:\> COPY autoexec.bat autoexec.BAK Copies specified file or files. Can be used to copy files to duplicates with different file names or to copy files into a different directory without changing names.
DEL C:\> DEL autoexec.BAK
C:\> DEL C:\backups\*.BAK
Deletes a file or files. Can be used to delete files in the current working directory or in some other directory. Can also be used with wildcard characters to delete groups of files.
DIR C:\> DIR
C:\> DIR c:\backups\*.txt
Displays all contents -- files and directories -- in the specified directory. If no directory is specified, it refers to the contents of the current directory. It can also be used with wildcard characters to display only specific files.
EDLIN C:\> edlin autoexec.bat Starts edlin, a line editor that can be used to edit text files. In this example, it is being used to edit the autoexec.bat file.
FORMAT C:\> FORMAT a: Formats a disk so it can be used with MS-DOS.
MKDIR C:\> MKDIR c:\NewDIR Creates a new directory in the specified path.
MORE C:\> MORE autoexec.bat Displays contents of a file, one screen at a time. It is mostly used for text files.
PROMPT C:\> prompt $p $d$g
C:\UTIL Fri 11-05-2021>
Modifies the default prompt. In this example, parameters are used with the command to set the prompt display as the current drive and path, the day and date and the greater-than symbol. The resulting prompt is shown in the example.
RMDIR C:\> rmdir c:\backup Removes a directory.
TYPE C:\> type autoexec.bat Displays content of a text file, without page breaks.

CLI versus GUI

The graphical user interface is the most popular user interface today. A GUI uses windows, menus and icons to execute commands. Using a mouse is the most common way to navigate through a GUI, although many GUIs allow some navigation and program execution via a keyboard.

Microsoft Word is an example of a GUI-based application. A user can change options for page layouts and styles by selecting the corresponding icon or pull-down menu with a mouse or keyboard.

One advantage of a GUI is the interface can visually display available functions. However, because it relies on graphical display, a GUI may not have the same level of functionality and granular control as a command-line interface. For example, it can require numerous clicks and movement through several dialog boxes in a GUI to accomplish the same result as a single command line.

In addition, GUIs do not readily support scripting or automation. For common tasks, a user must repeat each click or navigate each dialog within the GUI manually.

System administrators who may need to manage hundreds -- or hundreds of thousands -- of systems and configurations will find a GUI far less efficient than a CLI. A simple CLI command can easily adjust configurations for a large group of systems at once.

Commands and arguments can also be combined and saved, then executed as a script each time that specific action -- or comprehensive set of actions -- is required. The CLI is the preferred tool for many enterprise-wide systems management tasks.

CLI advantages and disadvantages

The following are advantages of a command-line interface:

The disadvantages of a command-line interface are the following:

The command line is a great place to become an expert at using a computer. Once you're ready, it's also the only place to learn how to write shell scripts.

03 Dec 2021

All Rights Reserved, Copyright 2000 - 2024, TechTarget | Read our Privacy Statement