More Command Line

More Command Line #

In this reading, we cover a few more command-line topics.

Efficiently Navigating the Command Line #

You can use the Up/Down arrows keys to go back through previous commands that you have run, which can be very helpful if you have mistyped a command and want to fix it.

You can also use Tab to autocomplete parts of your command (usually files/folders). If there are multiple possible completions, the options will typically be shown to you. You can then continue typing further until there is only one possible completion left, and then hit Tab to autocomplete.

Command Line Terminology #

In this course, we will be using the Bash shell for our command-line needs. A shell is a program that provides a way of interacting with your operating system’s services, such as managing files, running programs, and accessing networks. Bash is a commonly used shell that uses a command-line interface.

You may hear the terms command line, shell, and Bash, along with another term, the terminal, used interchangeably, even though they are slightly different. There are many programs that are shells, of which Bash is just one. The command line is the interface used to interact with Bash, and the terminal is the program that runs the window in which the shell runs. Specifically, starting a terminal opens a window and immediately starts the shell (Bash in this case) in that window. The program is more specifically a terminal emulator, because it emulates the older terminals that were once used to manage computer input and output.

Bash’s main feature is a read-evaluate-print loop (REPL), which repeatedly does the following steps in order:

  1. Read: as you type, the terminal emulator translates your keystrokes into input for the shell, usually a sequence of alphanumeric characters. You will see this text appear in the terminal window, and you can send your typed text to be read by the shell by pressing Enter.
  2. Evaluate: the shell interprets the sequence of characters you sent as a command and runs the appropriate computation.
  3. Print: if the computation produced any output for the user, the shell displays this output.

If you read the above paragraphs and are still fuzzy on the exact differences among all of the terms, that’s okay. We have tried to be precise with our terminology, but these terms are often confused even by people who have worked in computing for many years. If you search for help on the Web, it may be useful to know these differences, but you will not need this knowledge to complete any of the exercises in, or to submit, any assignment.

The Root User #

Sometimes, you may see Bash commands written like this:

# ls

The # indicates that you should run the command with administrative privileges. This is sometimes called running a command “as root” or “as the superuser”. Be very careful when running commands like this - this will let you do almost anything with your system and can cause catastrophic, irreversible changes.

Pattern Matching #

Pattern matching in Bash can be used to make your commands more succinct by specifying a pattern of files to operate on rather than each file individually. In this reading, we will examine a simple kind of pattern matching.

For any command in Bash, you can use * to perform a wildcard match. For example, if you have a directory containing files foo and bar, then the command cp * .. will copy both foo and bar to the parent directory. The wildcard can be used in conjunction with other text, so if a directory contains directories foo, bar and baz, then mv b* foo will copy both bar and baz into foo.

Wildcards only match with the contents of a single directory, so * cannot refer the special directories . or .., or to files in parent or child directories. To refer to all files in a child directory foo, you can use foo/*.

Note that when you run a command with *, Bash expands the wildcard match into its values before doing anything with the command. Thus in a directory containing only a directory foo, running cd * will turn into cd foo and move into foo. (If the directory contains multiple sub-directories, the command will likely result in an error instead.)