UNIX Commands

The command line

The shell executes a program when you give it a command in response to its prompt. For example, when you give the ls command, the shell searches for and executes the utility program ls. You can cause the shell to execute the C++ executable programs you create in CSCI 241 in much the same way. The line that contains the command, including any arguments, is called the command line.

Command-line syntax

Command-line syntax dictates the ordering and separation of the elements on a command line. When you press the ENTER key after entering a command, the shell scans the command line for proper syntax. The general format for a command line is:

   command [arg1] [arg2] ... [argn]

The square brackets in the example enclose optional elements. One or more SPACEs or TABs must appear between elements on the command line. The command is the command name, arg1 through argn are arguments, and the ENTER key is the keystroke that terminates all command lines (until you press the ENTER key, you can backspace and re-type the command as much as you like). The arguments are enclosed in square brackets above to show that they are optional: not all UNIX commands have arguments; some commands do not allow arguments, other commands allow a variable number of arguments, and others require a specific number of arguments in a specific order.

Command name

Some useful UNIX commands consist of only the name of the command. For example, ls without any arguments lists the contents of the working directory. However, most UNIX commands accept one or more arguments. Commands that require arguments typically give a short error message when you use them without arguments or use the wrong type of arguments.

Arguments

An argument is a filename (absolute or relative pathname), string of text, number, or some other object that a command acts on. For example, the argument to a nano command is the name of the file you want to edit.

The following command line shows the cp utility copying the file named temp to tempcopy:

   z123456@turing:~$ cp temp tempcopy

The UNIX shell prompt is shown to the left of the command. The command name is cp. The cp utility requires two arguments on the command line. The first is the name of an existing file to copy, and the second is the name of the copy of the file that it is creating. Here, the arguments are not optional; both arguments must be present for the command to work. If you do not supply the right number or kind of arguments, cp displays an error message:

   z123456@turing:~$ cp
   cp: missing file operand
   Try `cp --help' for more information.
   z123456@turing:~$

Options

An option is a special kind of argument that modifies the effects of a command. Frequently, you can specify more than one option, modifying the command in several different ways. Some options may be mutually exclusive: you can use one or the other, but not both. Options are specific to and interpreted by the program that the command calls.

By convention, options are separate arguments that follow the name of the command. Most UNIX utilities require you to prefix options with a hyphen. However, this requirement is specific to the utility and not the shell.

If you need to use several options, you can usually (but not always) group them into one argument that starts with a single hyphen; do not put spaces between the options. Specific rules for combining options depend on the utility. Most utilities allow you to list options in any order.

Example: We want to use the ls command with both the -a and the -l options. Any of the following command lines will work.

   ls -a -l
   ls -l -a
   ls -al
   ls -la