Previous Table of Contents Next

Module 23


This module discusses the different aspects of how UNIX and the shell handle commands. There are five areas of discussion:

*  Simple Command Syntax
*  Shell Command Syntax
*  Command Execution
*  Internal and External Commands
*  Return Codes


This section describes the syntax used to invoke a single UNIX program or utility, i.e., simple command. The following example explains the syntax in detail.

     name [ -option ... ] [ cmdarg ... ] where:
[] Brackets enclose an option or cmdarg that is optional.
... Periods indicate multiple options or cmdargs may follow.
name Is the filename or full path of an executable command.
option Preceded by a "-" (hyphen). Must be one of the following.
noargletter ... A single-letter option. Multiple noargletters can be grouped after one "-". Perhaps explaining the noargletter word would help. The noarg part means no arguments will follow the letter.
argletter optarg[,...] A single-letter option requiring an option argument. This is to say an argument will follow the option. Bascially, the option informs the program how to use the following argument.
optarg An optarg is a character string that is an argument to the previous argletter option. See rule 8 for further syntax rules.

BSD (Berkeley)
Command options in BSD may have to be separated by spaces. For example, on System V you might have -aik, whereas on BSD you would have to specify -a -i -k. Unfortunately, BSD has not implemented a standard on command line formats. Check the INTRO section of your system User's Reference Manual for command definitions.

cmdarg A character string not beginning with a "-". Often a filename path. A "-" by itself represents the standard input.
| A vertical bar may be placed between parameters to create a conditional "or" selection of parameters. Meaning either the preceding parameter or the following parameter may be used but not both.

The long form of a command is written as follows:

  name [ -noargletter ... ] [ -argletter optarg ... ] [ cmdarg ... ] The following example is
  described to help you understand how the shell interprets a command.

  date [ mmddhhmm[yy] ] | [ +format ]

The command name is date. The syntax shows that all parameters are optional. It is not necessary to provide any options. If the first argument is used, then yy is optional. The vertical bar represents an "or" condition in the parameter list. Therefore, either the first parameter or the second one can be used but not both at the same time.

Another command might be worth looking at:

     cat [ - ] [ -su ] [ -v [ -et ] ] [ file_list ]

The command name is cat. All parameters are optional. The - must be surrounded by blanks. The -v must be specified if either the -e or -t option is specified. The s, u, v, e, and t options may all be placed after the same hyphen (-). The command syntax is intended to show the dependencies of arguments and options, which is not necessarily the final format.

Command Syntax Rules

The following list of rules will be enforced on all releases of UNIX System V from Release 3.0 and later; although, there are some commands that existed before the 3.0 release that do not conform to these exact rules. The getopts command provides command parsing for shell procedures. It will check for legal options. It supports all but rules 1 and 2, which must be enforced by the command name itself.

1.  A command name must be in the range of two to nine characters long.
2.  A command name must contain only lowercase characters and numbers.
3.  Each option (noargletter and argletter) must be only one character long.
4.  Each option or group of options must be preceded by a "-".
5.  All Noargletter type options may be grouped together after one "-".
6.  The optarg list must be preceded by at least one blank or white space.
7.  An optarg cannot be optional. If an argletter is specified, then an optarg must follow.
8.  Multiple optargs must be separated by commas or must be separated by white spaces and quoted (e.g., -o abc,def or -o "abc def").
9.  Options must precede cmdargs.
10.  A "-" string may be used to indicate the end of the options.
11.  The relative order of the options should be irrelevant.
12.  The relative order of the cmdargs may affect how the command functions. The order may be significant.
13.  The " - " (a hyphen preceded and followed by blanks) should only be used to indicate the standard input.


The shell's sophisticated programming language allows complete control of command execution and combination of commands. The shell supports multiple types of commands providing for a flexible and powerful command and programming language.

Simple Commands

A simple command is a list of words. The general format of a simple command follows. For a complete description refer to the Simple Command Syntax section of this module.

     command options arguments

The first word is the name of the program to be executed. All remaining words are passed to the program as positional arguments. The first word equates to positional parameter 0. The following example illustrates this format.

     cj> cat -s file

cat is positional parameter 0, -s is positional parameter 1, and file is positional parameter 2.

Previous Table of Contents Next