Command Loop
- Contributors
One of the oldest and most iconic ways of interacting with a modern computer, the Command Loop is a way to issue commands, via plain text, directly to the operating system. Before graphical user interfaces, the command loop was the most common way to interact with a computer. Now, command loops have fallen out of favor with typical users, only seeing use from advanced users.
Typical Appearance
Though the aesthetics of the command loop vary slightly across many different platforms, almost all command loops are some variation on this:
Command loops usually consist of a history of previous commands and outputs, listed chronologically top to bottom, followed by a prompt for the next command. Command loops often also display the current directory such as this example:
Typical Behavior
A command loop is used to issue commands directly to the operating system. As such, the basic behavior of a command loop varies little across platforms. The command loop begins with the prompt. The prompt allows the user to type in and enter the command they wish to execute with any parameters they wish. The command loop then executes the command, and upon completion it returns to the prompt. Command loops often have the concept of a current directory, which is the directory within the computer's file system that commands given to the loop execute relative to. The command entered into the command loop is usually a single word (a series of characters not including a whitespace). Any subsequent words are parameters passed to the command. Words are usually delimited by spaces.
States:
Prompt
The basic state of the command loop is the prompt. The prompt is usually signaled by a symbol, such as >, $, #, or %, followed by a cursor, which is usually either a flashing underscore (depicted above), a flashing vertical bar, or a flashing rectangle. The prompt symbol indicates that the command loop is ready to accept commands, and the flashing cursor indicates that the user can type into the prompt. After the user submits the command (usually via the "enter" or "return" keys on the keyboard), the command loop enters the execution state.
Execution
After a command is issued to the command loop, it executes the command. The command loop can only execute one command at a time, so while it executes a command, it cannot take new commands (this makes the execution state and the prompt states distinct). Since, while in execution, the command loop cannot accept commands, it does not display the prompt symbol. It does, however, show the blinking cursor on a new line. The new line shows that the previous command has been executed (it is now part of the history), but the absence of the prompt symbol shows the command loop isn't accepting new commands.
State Diagram
Since a command loop only has two states, its state diagram is very simple. It simply provides the command prompt, and upon receiving a command,
Component in Action
Command Loop Functionality
Manipulate Files
Command loops have the power to copy, move, remove, and create files in directories. The commands for those functions vary depending on the shell, however the ability to edit files of a system is core to a command loop's function. All files within a certain set of permissions are allowed to be edited.
Edit Permissions
All files in a system have a list of permissions for itself depending on the user attempting to access the file. Permissions syntax varies depending on the system, however the general permissions which are granted to the user are read, write, and excecute.
Input/Output Redirection
Command loops recieve input from a single input source, this is referred to as the standard input of the shell. Generally, the standard input of a command loop is the keyboard, however it is possible for the prompt to read commands from a source that is not the keyboard. In addition, the standard output for a command loop is within its prompt which can also be changed. This process of temporarily changing the standard inputs and outputs for a command loop is called I/O Redirection (Input/Output Redirection). The main I/O redirects than can be performed are: redirect output, append, redirect input, and piping. I/O redirection syntax is generally consistent between different command loops.
Redirect Input command < file.txt
The <
character indicates to the command loop that the command should take its input from the file.txt instead of the keyboard. Other commands can be given in the file or the command can act upon the file given.
Redirect Output command > file.txt
The >
character indicates to the command loop that when the command is executed the output should be placed in the indicated file. If there is no such file in the directory then a new one is created. If such file does exist, the contents of the file are overwritten with the new output. Since the output of the command was redirected to the file, no results appear on the display.
Output can be added without overwriting the rest of the file with an append command
Append command >> file.txt
When results are appended to a file, it is added to the end of the file. If said file does not exist when calling the command then the file will be created.
Pipes command1 | command2
Piping is the most used tool by command loop users. Piping allows commands to be strung together with the standard output for the first command becoming the standard input for the second command. Filtering commands are frequently utilized with piping.
Pattern Matching
Command loops recognize Regular Expressions in order to specify specific or general input. This sort of tool makes automation possible. Here are a few universal regular expressions:
*
Matches andy characters
?
Matches any single character
[characters]
Matches any character that is an element of the set "characters".
[!characters]
Matches any character that is not an element of the set "characters"
These expessions can be linked together to search specifically as well: b*.txt
will search for input that begin with "b" and end with ".txt"
Command loops generally adhere to Portable Operating System Interface (POSTIX) standards. One of these universal standards are character classes: [:alnum:]
Alphanumeric characters, [:alpha:]
Alphabetic characters, [:digit:]
Numerals, [:upper:]
Uppercase alphabetic characters, and [lower:]
Lowercase alphabetic characters.
Variants
Though there are slight variations in appearance as well as specific functionality, almost all command loops feature the same methods of interaction. They all have a prompt that accepts commands as text, they all wait for the command to be executed, and they all return to the prompt after the execution is finished. Almost all command loops take commands and parameters as whitespace-delimited words and most require the enter or return keys on the keyboard to submit commands.
Priority Metrics
Command loops are, in some ways, the "anti-user interface". They are very basic and were almost completely supplanted by graphical user interfaces, which are considered much more intuitive to users. When looking at the usability metrics, the command loop struggles in all (except, arguably, satisfaction) but efficiency, where it soars. It makes sense, then, that it was replaced by GUIs for most users (where it wins in almost all other metrics), yet is still used by super users who value efficiency above all else.
Efficiency
Efficiency is where the command loop soars. Because the command loop lags in the other priority metrics, most are made with features that really cater to efficiency. A command loop is entirely textual so it does not require the use of the mouse; quick typing is the only motor skill that benefits the user of the command loop. Many commands in command loops are designed with short, abbreviated names to make them more efficient to type, such as ls
, cd
, and rm
. Command loops are also often made with features that increase efficiency. One such feature is tab completion. With tab completion, the user of the command loop can type the first few letters of a command or filename and hit tab to have the command loop fill in the rest of the phrase based on the closest match. Command loops vary in their handling of multiple matches, but most allow the user to see the matches, or hit tab repeatedly to cycle through them, so the user doesn't lose their "flow". Command loops also often allow the user to quickly enter previously entered commands via the up arrow on the keyboard. In many command loops, a user can hit the up arrow to get the previously given command and continue to press the up arrow to get more and more previously given commands.
Learnability
A command loop is either very easy to learn or very difficult to learn depending on one's perspective. In the basic sense, it is a very simple interface; most users know how to type, most users know how to hit enter, and most users know how to wait. However, in practice, a new user very rarely sees a command loop and knows what to do with it. They see a blinking cursor and know they can type, but they don't know what to type. Some command loops offer a list of possible commands internally...if you know the command to summon the list. To really know the possible commands to issue, most command loops require some kind of external reference material. Also, functionality such as tab completion and accessing previously issued commands with the up arrow are not readily apparent from most command loops. These functions either have to be taught to the user by a tutor of some kind, or discovered accidentally. For the most part, an unexperienced user cannot reasonably be expected to be given a command loop and to even figure out how to use it properly on their own.
Memorability
Once again, a command loop can either be very memorable or very non-memorable depending on one's perspective. The ability to type, hit enter, wait, and repeat is very memorable. But remembering commands is less so. Since there is no graphical interface, the user must remember specific text in the form of commands in order to use the command loop. Remembering strings of text is much more difficult for visually-inclined human beings than recognizing a button has a certain kind of functionality. Certain commands are very memorable because they have such short names, such as ls
, but they are still text that needs to be remembered. In addition, remembering the command alone isn't enough. Commands often take parameters and flags
that change their behavior, that often vary between commands and are hard to remember. Fortunately, many command loops offer a built in "manual" or "help document" of each command so a user who remembers a command but has forgotten exactly how to use it can easily look it up. This, however, is not memorability but a remedy for poor memorability.
Errors
Errors are a common problem with command loops. Making an error using a command loop is as easy as mistpying a word. Sometimes errors are hard to recognize as well. If a command is given with an unintended parameter, the command will still execute and the user may not even realized he or she even made an error.
Satisfaction
Satisfaction is an especially hard metric to judge with command loops. What some users find satisfying others may not. Some users really enjoy graphical interfaces, and get much more satisfaction from seeing things and touching them than typing text. Also a command loop is generally not very satisfying when it successfully completes a command because it just returns to the prompt without giving feedback. On the other hand, some users find using a command loop very satisfying because they feel very efficient or they "feel like a hacker".
Key Characteristics
The Prompt
A good command loop has a prompt that clearly shows that the loop is accepting commands. It should have a blinking cursor.
Tab Completion
Tab completion is an incredibly efficient feature and all command loops should have it
History
A good command loop should display previously issued commands and should make them easy to access via the up arrow or some similar key on the keyboard
No Mice
There is no such thing as a "command loop with a GUI". The command loop fails in so many of the usability metrics because it excels in efficiency. Requiring the use of a mouse hampers efficiency, the one thing command loops have going for them, and should not be a part of them
Bash
Bash is the command language developed for the GNU Project. Since its release in 1989, bash has found itself as the default command line interface shell (CLI) for many Linux distributions and Apple's OS X.
History
Bash was written in 1989 by Brian Fox as a replacement for Bourne Shell (sh), the default CLI for Unix systems at the time. (rollover "Bash refers to Bourne-Again SHell) Fox designed bash to be backward compatible with sh scrips while adding in features from other command shells Korn shell (ksh) and C shell (csh) such as command line editing, unlimited size, command history, shell functions, aliases, indexed arrays of unlimited size, and integer arithmetic in any base from two to sixty-four.
Permission Structure
Each file has certain rules applied to itself depending on the type of user attempting to access it. The user types owner, group, and everyone can be given read, write, and execute permissions on said file.
Below is a visual representation of what file permissions look like in bash.
*r,w,x standing for read, write, and execute accordingly.
It is easy to think of the permission settings as a series of bits:
rwx rwx rwx = 111 111 111
rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000
chmod
chmod is a command to edit the permissions of a file. It's options are represented in decimal due to permissions being represented in binary:
rwx = 111 in binary = 7 in decimal
rw- = 110 in binary = 6 in decimal
r-x = 101 in binary = 5 in decimal
r-- = 100 in binary = 4 in decimal
-wx = 011 in binary = 3 in decimal
-w- = 010 in binary = 2 in decimal
--x = 001 in binary = 1 in decimal
--- = 000 in binary = 0 in decimal
and can be used by chmod as follows: chmod 600 some_file
which would give only the owner read and write permissions on the file.