1.7. Unix Command Basics

A Unix command is built from a command name and optionally one or more command line arguments. Arguments can be either flags or data.

	ls -a -l /etc /var
	^^ ^^^^^ ^^^^^^^^^
	|   |    |
	|   |    Data Arguments
	|   Flags
	Command name
	

For many Unix commands, the flags must come before the data arguments. A few commands require that certain flags appear in a specific order. Some commands allow flags and data arguments to appear in any order. Unix systems do not enforce any rules regarding arguments. How they behave is entirely up to the creator of the command. However, the vast majority of commands follow conventions, so there is a great deal of consistency in Unix command syntax.

The components of a Unix command are separated by white space (space or tab characters). Hence, if an argument contains any white space, it must be enclosed in quotes (single or double) so that it will not be interpreted as multiple separate arguments.

Example 1.1. White space in an Argument

Suppose you have a directory called My Programs, and you want to see what's in it. You might try the following:

shell-prompt: ls My Programs
	    

The above command fails, because "My" and "Programs" are interpreted as two separate arguments. In fact, the ls command will look for two separate directories called "My" and "Programs". In this case, we must use quotes to bind the parts of the directory name together into a single argument. Either single or double quotes are accepted by all common Unix shells. The difference between single and double quotes is covered in Chapter 2, Unix Shell Scripting.

shell-prompt: ls 'My Programs'
shell-prompt: ls "My Programs"
	    

As an alternative to using quotes, we can escape the space by preceding it with a backslash ('\') character. This will save one keystroke if there is only one character to be escaped in the text.

shell-prompt: ls My\ Programs
	    

Practice Break

Try the following commands:

shell-prompt: ls
shell-prompt: ls -al
shell-prompt: ls /etc
shell-prompt: ls -al /etc
shell-prompt: mkdir 'My Programs'
shell-prompt: ls My Programs
shell-prompt: ls "My Programs"
	    

1.7.1. Self-test

  1. What are the parts of a Unix command? What separates them?
  2. What are command line flags?
  3. What are data arguments?
  4. What rules do Unix systems enforce regarding the placement of arguments?
  5. How do we specify an argument that contains white space?