5.3 Command line structure
UNIX Commands Structure
To give a command to a UNIX system you type the name of the command, along with any associated information, such as a filename, and press the <Return> key. The typed line is called the command line and UNIX uses a special program, called the shell or the command line interpreter, to interpret what you have typed into what you want to do. The components of the command line are:
- the command;
- any options required by the command
- the command's arguments (if required).
command [-option(s)] [argument(s)]
NOTE: Options MUST come after the command and before any command arguments. Options SHOULD NOT appear after the main argument(s). However, some options can have their own arguments! Historically, UNIX commands have been fairly standard in the way that they use options but there are variations - so be aware!
Since the introduction of UNIX System V, Release 3, any new commands must obey a particular syntax governed by the following rules:
- Command names must be between 2 and 9 characters in length
- Command names must be comprised of lowercase characters and digits
- Option names must be one character in length
- All options are preceded by a hyphen (-)
- Options without arguments may be grouped after the hyphen
- The first option argument, following an option, must be preceded by white space. For example -o sfile is valid but -osfile is illegal.
- Option arguments are not optional
- If an option takes more than one argument then they must be separated by commas with no spaces, or if spaces are used the string must be included in double quotes ("). For example, both of the following are acceptable:
- -f past,now,next and -f "past now next"
- All options must precede other arguments on the command line
- A double hyphen -- may be used to indicate the end of the option list
- The order of the options are order independent
- The order of arguments may be important
- A single hyphen - is used to mean standard input
You should bear in mind that commands established before System V, Release 3, do not conform to all of the above rules.
3.2 Unix Command Line Structure
A command is a program that tells the Unix system to do something. It has the form:
command [options] [arguments]
where an argument indicates on what the command is to perform its action, usually a file or series of files. An option modifies the command, changing the way it performs.
Commands are case sensitive. command and Command are not the same.
Options are generally preceded by a hyphen (-), and for most commands, more than one option can be strung together, in the form:
command -[option][option][option]
e.g.:
ls -alR
will perform a long list on all files in the current directory and recursively perform the list through all sub-directories.
For most commands you can separate the options, preceding each with a hyphen, e.g.:
command -option1 -option2 -option3
as in:
ls -a -l -R
Some commands have options that require parameters. Options requiring parameters are usually specified separately, e.g.:
lpr -Pprinter3 -# 2 file
will send 2 copies of file to printer3.
These are the standard conventions for commands. However, not all Unix commands will follow the standard. Some don't require the hyphen before options and some won't let you group options together, i.e. they may require that each option be preceded by a hyphen and separated by whitespace from other options and arguments.
Options and syntax for a command are listed in the man page for the command.