labs / tiddlers / content / labs / 01 / _Labs_01_Command-Line Interface (CLI).md


Linux supports modern desktop-style graphical user interfaces, but it also provides a powerful textual shell or command-line interface (CLI). Such a command-driven interface might seem somewhat old-fashioned, but it has a number of benefits:

  • Commands can be precisely and concisely expressed as text.
  • Commands can easily be recalled and modified during testing and development.
  • Commands can easily be shared and replayed by yourself or others.
  • Groups of commands can be saved in scripts or functions for flexible reuse.
  • General programming features (variables, loops, conditionals, functions, etc.) are also available, allowing powerful automation of tasks.
  • Input and output can be redirected to/from files, using the > and < operators.
  • Commands can be combined into pipelines (where the output of one program is used as the input to another) for more complex operations. This is done using the | operator.

Technically, the commands are initially parsed and processed by the shell, commonly bash, the "Bourne-again shell". Interactive shell input and output is handled by software such as Sakura or xterm that emulates an old-style terminal.

The basic interaction for the CLI is as follows:

  • If the prompt (usually $) is shown with the text cursor following it, the shell is ready for your input.
  • Compose a line of text, and press Enter (or Return) to run it. The command name itself will be the first element, and other arguments such as settings flags or input/output file names may follow.
  • If the command runs successfully, you might or might not see any output. Some commands (such as mkdir for creating a new directory or folder) work entirely by side-effects and do not normally produce any output.
  • If there was an error, a message will normally be printed. Read and try to understand the error before proceeding.
  • Whitespace is used to separate keywords (tokens) on the command line. Files or folders with names containing spaces can cause problems. You may have to resort to (single- or double-) quoting them or using the escape character, \, before the space.

Some useful command-line tips and tricks:

  • The mouse will be of limited use, so get used to keeping your hands over the keyboard. The keyboard does provide some useful shortcuts, however.
  • Use the Tab key to auto-complete the names of files, folders and commands. This helps avoid typos and misspellings, and is also a useful time-saver. You might have to press Tab repeatedly in some situations (e.g. if there are multiple matches).
  • Use Alt + left/right arrow to move by word.
  • Ctrl-A and Ctrl-E will jump the cursor to the start and end of the line respectively.
  • Ctrl-W will delete the preceding word.
  • The shell maintains a command history. Use the up and down arrow keys to navigate the history.
  • Copy and paste work a little differently. Ctrl-C is already reserved for the kill signal
  • Use Ctrl-D to signal end-of-file to a command that expects text input.
  • Use Ctrl-C to interrupt or kill a process that might be stuck (sends the SIGINT signal).
  • Use Ctrl-Z (SIGSTOP signal) to suspend the current process (if any), and bg to have it continue running in the background. You can also use & at the end of the command line to have it run in the background immediately. Use fg to bring it back to the foreground (for further interaction).

Getting Around

TODO: things like cd, pwd, path expressions, relative and absolute paths