“A command-line interface, or CLI, is a method for users to issue text based commands to a computer system”
Most users of modern computer systems interact with software using a graphical user interface, or GUI. Before the widspread use of GUIs, computers were typically utilised by issuing lines of text to the computer as command-line operations, which were then processed by the computer. Although many programs can provide their own command line interface, many leverage the CLI provided by the operating system on their device. Other common names for command line interface may include terminal, terminal emulator, console, shell, command line interpreter, or interactive shell.
Common shortcomings of the command lines usability include remembering the available commands and how to use them, as well as their requirements for syntax adherance, which means that typing errors result in program errors. Some scenarios where CLIs are very useful include:
Most operating systems have their own form of commandline console, such as:
Goal: To explore how to issue commands to a computer
Open your operating systems command line interface or terminal. If you are unsure how to do this, try searching the internet for instructions on your operating system.
help
help | more
--help
and on windows the /?
is often used.dir --help
or dir /?
depending on your operating systemThe commands available and the syntax (the structure and formatting of how instructions are written) will vary based on the system being used. Many systems offer multiple ways of performing the same commands, often termed aliasing, which simplifies the command issuing process and prevents errors. For example, the following examples illustrate the same instructions being issued on different CLIs.
Command-Prompt:
echo Some Text
set VARONE=Variable One Content
echo %VARONE%
Bash:
echo Some Text
VARONE=Variable One Content
echo $VARONE
Powershell:
Write-Output('Some Text')
echo $VARONE
$VARONE = "Variable One Content"
Set-Variable -Name "VARTWO" -Value "Variable Two Content"
Write-Output($VARTWO)
Most instructions or instruction sequences contain the following components:
One concept imperative to proper use of the command line is undertanding file paths and directory structures.
In the command line, often the "relative" path, or the file path of the "current" directory, is represented as a dot or fullstop, .
Using a double dot, ..
, indicates the parent directory.
You can try see how this works by using the command cd ..
to move to the parent directory of the current path
Paths are structured a bit differently between different operating systems.
On Windows, the system directory is usually C:\Windows and the home directory of the current user is C:\Users\Username
The windows commandline displays the currently active path in the follwoing format C:\File\Path>
whilst Windows PowerShell differentiates itself from the CMD interface by prefixing the path with PS such as in PS C:\File\Path>
.
Linux typically identifies the user, host and path in a format such as currentuser@hostname:/file/path/$
. Linux also typically uses the path /
for the system directory and /home/username/
, usually represented by the tilde character ~/
for the currently active user directory.
Linux does not mount separate disks like windows, and to change disks in windows CMD users will have to input the drive letter followed by a colon, such as d:
in order to traverse the filesystem on that drive.
Instructions are commonly used in a read–eval–print loop (REPL) format. This process executes in the way it is described, in that it reads the inputs, evaluates the response and returns it (print or echo).
Command-Prompt:
set "PREFIX=Well, hello there"
set "SUFFIX=!"
set /p NAMEVAR="What is your name: "
echo %PREFIX% %NAMEVAR%%SUFFIX%
Bash:
PREFIX="Well, hello there"
SUFFIX="!"
read -p "What is your name: " NAMEVAR
echo $PREFIX $NAMEVAR$SUFFIX
Sometimes commands on different systems may require the use of different flags. The following example sends an ICMP request to a server and waits for the response. On Windows, you specify the Number of times to ping the server, whereas on Linux you specify the Count of pings. The concepts are usually similar, however the syntax may vary. You can use the --help
or /?
commands to learn how to use the command on each system.
Command-Prompt:
ping google.com -n 10
Bash:
ping google.com -c 10
You can also cancel currently executing commands, such as using the CTRL+C keyboard shortcut.
Commandline instructions can also be written into system executable's. These are simply text files with instructions that tell the interpreter what instructions to execute and in which order.
On Windows, these files typically carry file extensions like .bat .cmd and are often called "batch" files.
On Linux, the convention is to use .sh as a file extension but any text file can be marked as an executable.
You can then use programs like cron or the windows task scheduler to run these instructions on a schedule or at predetermined times.
Similar instructions can also be bundled into a configuration file and executed by another program, such as a makefile or python script.
A CLI is:
Using a CLI allows users to:
CLI variables are:
Setting the windows PATH: https://www.computerhope.com/issues/ch000549.htm
CMD Cheatsheet: https://dev.to/moniruzzamansaikat/windows-command-cheatsheet-hce