What's a Command-Line Program?

In this lesson we’ll discuss what command line programs are and look at an example. We’ll also learn about command line arguments.

A command-line program is a program that you run in a terminal or console. It may write some output to the console, too. Although the Unix principle of “Silence is golden” asserts no output if the command was successful, some command-line programs may be interactive. When you run them, they wait for the command and take action accordingly. Then they wait for the next action until you explicitly exit. Let’s discuss what types of input a command-line program takes.

Input

Programs can read the standard input stream and prompt the user for input interactively. However, we will focus on programs that use command-line arguments in the form of strings here. Semantically those can be divided into commands and subcommands, flags, and program arguments.

Most programs have a default action when you run them with no arguments, and they display a usage message if you run them with no input. For example, git displays its help:

git

Commands

Check out this command-line git stash. When you run this command, it stores your changes to a dirty working directory. So, git is our program, and stash is a command that git understands. However, the stash has subcommands too. For example, git stash list shows all stashes. Here, stash is a command, and list is a sub-command of stash. Commands and subcommands are typically words. It’s helpful to think about commands and subcommands as verbs.

Flags

Flags, sometimes called options or switches, modify the commands behave. Flags often have a long form and a short form. The short form is a one letter prefix with a single dash like -a and the long form is a word prefixed by a double dash like --all. For example, to list all git branches, you can use the command-line git branch -a or git branch --all

Since there are no standards around command-line arguments, sometimes flags function as subcommands. For example, git branch -d <branch name> means delete the branch called <branch name> but one can argue that it is more natural to use a subcommand git branch delete.

Arguments

Arguments are the things the command operates on. However, some commands don’t need arguments like git status, which simply returns the status of the current repository. In this case, there is no need for any additional arguments, but most commands require some arguments. For example, the git add command adds files or directories to the staging area, so they are ready to commit.

The following shell script creates a couple of files, adds them on line 3, and checks the status.

touch file-1
touch file-2
git add file-1 file-2
git status

Output

Command-line programs typically write their output to streams. There is a standard output stream and standard error stream, and streams can be directed to different destinations (like files) to distinguish successful results from errors.

Pipes are a Unix mechanism where multiple programs are chained together such that the standard output of a program becomes the standard input of the next program in the pipeline. On the other hand, the standard error goes to the terminal unless redirected. To pipe programs, you add the vertical pipe symbol (|) between them.

For example, we can pipe the output of ls to wc to count how many entries are in the current directory:

ls | wc -l

However, if we pass a nonexistent path to ls it will produce an error, and wc will receive nothing. In this case, the output of the pipe will be zero, because wc got nothing, and the error from ls will be printed.

ls no-such-path | wc -l

Exit code

When a program exits, it returns an integer exit code. Here, zero means success, and non-zero means failure.

Other forms of output

The output and error streams are the bread and butter of command-line programs, but there are many other forms of output. A program can choose to write files, make network calls, send commands to devices connected to its host machine, or invoke other programs. If you’re a purist, you may consider these side-effects and not output, but it’s important to realize that command-line programs can do more than just simple text processing. In particular, git is all about managing files, and its index can interact with remote servers. Other good examples are ssh and the aws CLI.

Other program types

Command-line programs are very versatile as you’ve seen, but typically they have a relatively large vocabulary of commands. Users or other programs invoke them to execute one command, and then they exit.

Other programs are often long running because servers are a class of programs that wait for incoming requests or connections, process the requests, return the responses to the callers, and wait for the next request (or next packet). Web servers are a great example of servers that implement the HTTP protocol.

Programs with a visual user interface and games are yet another class of interactive programs that take their inputs from the keyboard, mouse, or other controllers and update the screen as output. These programs typically have a main loop and they refresh the display at a high rate to provide a smooth and high-fidelity user experience.

Quiz

Let’s finish this lesson with a little quiz… enjoy!

1

Command-line programs usually perform one command and exit

A)

True

B)

False

Question 1 of 30 attempted