Shell Command Options and Arguments

A shell command consists of the command name followed by possible options and arguments. Some commands require no options or arguments, for example, pwd, which displays the present working directory.

Options and arguments (what options are available, what arguments are required) are specific to the commands and not to the shell itself. Nonetheless, their format is fairly consistent.

Command Option Formats

Options affect the actions of a command. For example, ls without options simply displays the names of files and directories in the current working directory, while ls -l (long listing format) provides more detailed information.

Some options are specified with a single character. Other options are specified with a word (or hyphenated word). Some options can be specified with either. For example, the ls command includes an option to include hidden files in the listing which can be expressed as -a or --all:

ls -a
ls --all

Single character options are preceded with a hyphen; word-length options are preceded by two hyphens.

Some options require an argument. Word-length options typically require an equal sign with no surrounding whitespace between option name and value. With single-character options, a space typically separates option from value. The following demonstrates various ways to specify the number of lines to display when issuing the tail command to view the last lines of the .profile file:

tail -n 5 .profile
tail --lines=5 .profile

tail -5 .profile # can also preface number with - (no -n needed)

You can combine short options (as long as they require no arguments), but not long options. The following demonstrates two ways to specify a long listing with human readable file sizes for the ls command:

ls -lh

ls --human-readable -l

Two hyphens (--) separated by whitespace from other tokens, by convention, signals the end of options. What follows is an argument to the command, even if it looks like an option.

Command Arguments

Arguments required for a command are specific to the command, as mentioned above. If you issue a command with an incorrect or incomplete argument list, a brief usage message will be displayed informing you of the syntax and requirements of the command. The following shows an example usage message when you forget the destination for a cp command:

$ cp count.sh 
cp: missing destination file operand after 'count.sh'
Try 'cp --help' for more information.

A few more tips for command arguments:

  • When an argument includes spaces, enclose it in single quotes.
  • When a filename is expected as an argument, a hyphen signifies standard input.
  • Follow a command with & to run it in the background.

Back to top