Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Chapter 10...

Command Syntax

As you use the Unix CLI (command line interface), you enter one command after another. When you enter a command, the entire line you type is called the COMMAND LINE. When you press <Return> at the end of the line, the contents of the command line are sent to the shell to be processed.

As you enter a command, there are two things to understand. First, you must type the command properly according to certain rules. How to do so is the subject of this chapter. Second, you must understand what happens as the shell processes the command. This is an important topic, which we will discuss later in the book when we talk about the shell.

There are literally hundreds of commands, and as long as you work with Unix, you will never stop learning new ones. For that reason, you need to be able to teach yourself from the online manual as the need arises. In order to use the manual well, you need to understand the formal rules that describe how a command must be entered.

Jump to top of page

Entering More Than
One Command at a Time

In most cases, you will type only one command on the command line. However, you should know that it is possible to enter more than one command at a time. All you need to do is separate the commands with semicolons.

Here is an example. You are on the phone planning a party with one of your friends. You want to display today's date, as well as the calendar for the current month. The commands to do this are date and cal (see Chapter 8). Of course, you can enter the two commands separately:

date
cal

However, you can do it all in one line by using a semicolon as a command separator:

date; cal

Notice that you do not need a semicolon at the end of the command line.

Here is another example using the cp command to copy a file (see Chapter 25), the ls command to list information about a file (Chapter 24), and the rm command to erase (remove) a file (also Chapter 25).

Your goal is to make two copies of a file named data. The copies will be called backup1 and backup2. After you make the copies, you want to erase the original file. You then list all your files to see what you have. The commands look like this:

cp data backup1
cp data backup2
rm data
ls

Don't worry about the details. We will cover these commands later in the book. The important point here is that, instead of typing the commands on four separate lines, we can enter them on one line by separating them with semicolons:

cp data backup1; cp data backup2; rm data; ls

You might ask, what's the point of entering all the commands on one line? After all, you have to type the same number of characters either way.

The answer is that, in the long run, you will use Unix more effectively if you learn how to think ahead more than one step at a time, much like a chess player does as he decides on his next move. Although this may not seem like a big deal now, it will become important as you become more experienced.

In Chapters 15 through 19, I will show you how to put together commands and tools to form complex command lines. To do this well, you need to develop your skills as a Unix problem solver. For now, I'd like you to start thinking in this way by seeing how many opportunities you can find to enter multiple commands on the same command line.

— hint —

The difference between a good Unix user and a great Unix user is the ability to solve problems as they arise by putting together tools and ideas quickly, creatively, and with a minimum of fuss. In fact, some people would say that this is the essence of using Unix well.

Jump to top of page

What Happens When
You Enter a Command?

To enter a command, you type the name of the command, possibly followed by other information. For example, consider the following ls command line.

ls -l -F file1

This command has four parts: the name of the command, ls, followed by -l, -F and file1. (I'll be more specific in a moment about what these other parts are called.)

When you press the <Return> key, the shell processes your command. The actual details are a bit complex, and we will leave the discussion to later chapters. For now, here is a simplified description that works well for most people most of the time.

The shell processes a command line by assuming that the first part of the line is the name of a command you want to run. The shell then searches for and executes the program by that name. For example, if you were to enter the previous ls command, the shell would find and run the ls program.

If you enter the name of a command that the shell can't find, it will display an error message. For example, let's say you enter:

harley

Although it's hard to believe, Unix doesn't have a harley command, so you will see:

harley: not found

When the shell does find the program you want, it runs it. At that time, the shell sends a copy of the entire command line to the program. It is up to the program to figure out what to do with all the information. In the example above, it would be up to the ls program to figure out what to do with -l, -F and file1.

Jump to top of page

Command Syntax

A Unix command must be typed just so, according to well-defined rules. Putting a punctuation mark in the wrong place or spelling a word incorrectly can invalidate the entire command. Most of the time, you will see an error message and the command will be ignored. However, in the worst case, a mistyped command will execute incorrectly and cause problems. Thus, it is important that you learn how to enter commands correctly. The formal description of how a command should be entered is called its COMMAND SYNTAX or, more informally, its SYNTAX.

How do you learn the syntax for a command? It's easy. Once you understand the general rules and conventions, you can teach yourself how to use any command by looking up its syntax in the manual or in the Info system (see Chapter 9).

If you are a beginner, you know that there are many commands that, right now, are beyond your capabilities. Indeed, there are some commands you will never understand (or never need to understand). Regardless, my goal is that, by the time you finish this chapter, you should be able to understand the syntax of any command you happen to encounter, even if you don't understand the command itself(*).

* Footnote

This is not as odd as it sounds. For example, when you learn a foreign language, it doesn't take long to get to the point where you can read sentences you can't understand. The same is true for Unix. By the time you finish this chapter, you will be able to understand the syntax of any Unix command. However, until you get more experience, most commands will still be a mystery to you.

My goal is to make sure you have a firm grasp of the basic principles, such as command syntax. This will allow you to fill in the gaps by practicing regularly and taking the time to teach yourself. For example, whenever you have a question, I encourage you to take a moment to use the manual or the Web to find yourself an answer. (If this sounds like fun to you, you are a lucky person.)

As a general rule, Unix command syntax can be expressed as follows: You type the name of the command, followed by any "options", followed by any "arguments":

command-name options arguments

Options allow you to control how a command should do its job, while arguments specify the data you want the command to use.

Consider the example:

ls -l -F file1 file2 file3

The command name is ls. The options are -l and -F. The arguments are file1, file2 and file3.

Now, let's move on to the details.

Jump to top of page

Options

When you type a command, you use OPTIONS to modify how the command should do its job. When you read Unix documentation, you will sometimes see options referred to as SWITCHES or FLAGS, so you should know those words as well.

As the name implies, the use of options is optional. Most commands have at least a few, but some commands have so many that you wonder how anyone can ever know them all. The answer is no one does. Even experienced people depend on the manual to look up obscure options.

Within the command line, options come directly after the command name. An option consists of either a hyphen followed by a single letter, or two hyphens followed by a word. For example:

ls -l -F file1 file2 file3
ls --help

In the first command, there are two options, -l and -F. In the second command, there is one option, --help.

The job of an option is to allow you to control the actions of the command. In our example, for instance, the -l option tells the ls command to display the "long" listing. Normally, the ls command lists only the names of files. When you use the -l option, ls lists extra information about each file, as well as the names.

Occasionally, you will see an option that is a number, such as:

ls -1 file1 file2 file3

You will have to be careful. For example, in this case, do not confuse -l (the lowercase letter "L") with -1 (the number 1).

When you use more than one single-character option, you can combine them by using a single hyphen. Moreover, you can specify options in any order. Thus, all of the following commands are equivalent:

ls -l -F file1
ls -F -l file1
ls -lF file1
ls -Fl file1

As with all Unix commands, you must make sure that you use upper- and lowercase exactly. For example, the ls command has both -F and -f options and they are different. However, as a general rule, most options are lowercase. (As I explained in Chapter 4, almost everything in Unix is lowercase.)

— hint —

When you look up a command in the manual, you will see an explanation for each individual option. However, the man page will not tell you that single-character options can be combined. For example, -l -F can be combined into -lF. This is basic Unix, and it is assumed that you already know it.

At this point, an interesting question arises: When you talk about an option, how do you pronounce the hyphen?

In the olden days, it was pronounced as "minus", probably because it is easier to say than "hyphen". For example, an older person would pronounce ls -l as "L-S-minus-L".

Within the Linux culture, however, it has become common for people to refer to the hyphen as "dash", especially among younger users. Thus, a Linux person would probably pronounce ls -l as "L-S-dash-L". Similarly, "ls --help" would be pronounced "L-S-dash-dash-HELP".

The distinction between "minus" and "dash" is not important when you type a command. However, when you are talking, what you say out loud can have important social implications.

For example, let's say your teacher calls upon you during class, and asks you, "How do you tell the ls command to display a long file listing?" For most students, the answer would be, "Use the dash-L option." However, if you think your teacher learned Unix before the mid-1990s, show respect for an elderly person by saying, "Use the minus-L option." (Remember, one day you will be old yourself.)

Jump to top of page

Dash Options and Dash-Dash Options

When it comes to options, the single dash format is the older one. It dates back to the earliest Unix system (see Chapter 2), when programmers desired brevity on the command line. Many of the command names were only two or three letters, and all of the options were only a single letter.

Years later, when the GNU utilities were being developed (see Chapter 2), the designers wanted to be able to use longer options. However, the longtime conventions dictated that all options should only be one letter, and that options could be combined as I explained above. For example, what do you think would happen if the shell encountered the following command?

ls -help

Since multiple options can be combined, -help would refer to four separate options, -h, -e, -l and -p.

Of course, it would be possible to modify the rules, but that would be a drastic change that would invalidate many existing programs, and force too many people to change their habits. Instead, it was decided that longer options would be allowed, as long as they were preceded by two hyphens, instead of one. In that way, the system could be expanded without compromising what already existed.

Since Linux uses the GNU utilities, you will see both types of options if you are a Linux user. With most other systems, you will see only single-character options.

The chief purpose of what we might call the dash-dash options is to use longer option names, which makes them easier to understand and remember. On the other hand, longer options are slower to type and a lot easier to misspell. For this reason, many commands give you a choice by giving the same option a short and a long name. This allows you to use whichever one you want.

For example, the ls command (which lists file information) has a -r option, which causes the command to list the files in reverse order. With Linux, the command also has a --reverse option, which does the same thing. Thus, the following two commands are equivalent:

ls -r
ls --reverse

Within the world of dash-dash options, there are two options that you will often encounter when you use the GNU utilities. If you are a Linux user, these options are worth memorizing.

First, many commands will display a summary of the command's syntax if you use the --help. Typically, you would use this option by itself. For example:

ls --help
date --help
cp --help

In many cases, the summary is so large that most of it will scroll off your screen before you can read it. If this happens, send the output to the less program, which will display the information once screenful at a time. To do so, type a | (vertical bar) followed by less. For example:

ls --help | less
date --help | less
cp --help | less

(See Chapter 21 for a discussion of less.)

The second common dash-dash option is --version:

ls --version
date --version
cp --version

This option displays information about what version of this particular program is installed on your system. Normally, this is not information that you would need. However, if you are having a problem, it sometimes helps to know what version of the program you are running.

— hint —

The rules for typing options are the same for virtually all Unix commands: each option starts with either one or two hyphens; single-character options may be combined. However, there are a few exceptions.

Some commands use options without a hyphen. Other commands will accept a hyphen, but don't require it. Finally, some commands will not allow you to combine single-character options.

Fortunately, the exceptions are rare. However, if you have any problems with a command, check with the man page. It is the definitive reference.

Jump to top of page

Arguments

I mentioned earlier that the general syntax of a Unix command can be expressed as follows: the name of the command, followed by any options, followed by any arguments:

command-name options arguments

We have already talked about command names and options, so let us move on to discuss how to use arguments.

ARGUMENTS are used on the command line to pass information to the program you want to run. Consider this example, which we looked at earlier during our discussion of options:

ls -l -F file1 file2 file3

The command is ls, the options are -l and -F, and the arguments are file1, file2 and file3.

The meaning of an argument will vary from one command to another. Typically, the arguments specify the data upon which the program will perform an action. In our example, we are specifying the names of three files.

Consider another example:

man date

The command name is man, there are no options, and the argument is date. In this command, we are telling man to display the man page for date.

One last example:

passwd weedly

Here we are using the passwd (change password) command with one argument, weedly. In this case, we are indicating that we want to change the password for userid weedly (see Chapter 4).

What's in a Name?

Argument


The general syntax of a Unix command is:

command-name options arguments

The word "option" only makes sense because command line options give you a choice as to how the command should execute. However, what about the word "argument"? Obviously, it does not indicate an intellectual conflict or "a connected series of statements intended to establish a proposition"(*).

Actually, the computer term "argument" is borrowed from mathematics, where it indicates an independent variable. For example, in the equation f(x) = 5x + 17, the argument is x. In this sense, an argument is something upon which a command or a function operates.

In English, the word "argument" comes from the Latin word arguere, which means to clarify or to make clear.

* Footnote

If you don't recognize this quotation as coming from the Monty Python sketch "The Argument Clinic", you have a significant gap in your cultural training. My advice is to take a moment right now and correct this deficiency. Hint: Use the Web to search for:

"monty python" "argument clinic"

Jump to top of page

Whitespace

When you enter a command, you must make sure to separate each option and argument. To do so, you must type at least one space or tab between each separate part. For example, here are several ways of entering the same command. Notice that I have explicitly indicated where I pressed the <Space> bar and <Tab> key:

ls<Space>-l<Space>-F<Space>file1
ls<Tab>-l<Tab>-F<Tab>file1
ls<Space><Tab>-l<Space>-F<Tab><Tab><Tab>file1

Normally, of course, you would put a single space between each part of the command. In fact, with some shells, you can only use spaces, because tabs have a special function (called command completion). However, the idea of using spaces and tabs as separators is important enough to have its own name: whitespace.

This idea will come up repeatedly as you use Unix, so let me give you a formal definition. Within a command line, WHITESPACE refers to one or more consecutive spaces or (with some shells) tabs. In other situations, whitespace may refer to one or more consecutive spaces, tabs or newlines. (See Chapter 4 for a discussion of the newline character.)

If you are a Windows or Macintosh user, you will have seen file names that contain spaces. For example, Windows systems use folders (directories) with names like "Program Files", "My Documents", and so on. Within Unix, spaces in the command line are considered to be whitespace. As such, you should never use spaces in the name of a file. Similarly, you will never see a Unix command with a space in its name.

— hint —

When you choose names that have more than one part, you should never use a space in the middle of the name. However, you can use a hyphen (-) or an underscore (_).

For example, the following are all valid names:

program-files
program_files
my-documents
my_documents

My personal preference is to use hyphens, because they are more readable than underscores.

(Reminder: Unix distinguishes between upper- and lowercase. Also, most of the time, we use only lowercase for names.)

What's in a Name?

Whitespace


The term "whitespace" refers to consecutive spaces and tabs that are used to separate two items. The name derives from the earliest Unix terminals that printed on paper. As you typed a command, there was real white space between each word.

The Unix shell (command processor) was designed to be flexible; it didn't care how much space there might be between parts of the command line, as long as the parts were separated. Thus, the term "whitespace" came to mean any number of spaces and tabs.

Later, for certain applications, the term was extended to mean any number of spaces, tabs or newlines. (You will remember from Chapter 4 that a newline is the character generated when you press the <Return> key.)

Jump to top of page

One or More; Zero or More

In the next section, I will discuss the formal method for describing commands. Before I do, however, I need to define two important expressions: "one or more" and "zero or more".

When you see the expression ONE OR MORE, it means that you must use at least one of something. Here is an example.

In Chapter 9, I explained that you can use the whatis command to display a short description of a command, based on its entry in the online manual. When you use whatis, you must specify one or more command names as arguments. For instance:

whatis man cp
whatis man cp rm mv

The first example has two arguments; the second example has four arguments. Because the specifications for this command call for "one or more" names, we must include at least one — it is not optional.

The expression ZERO OR MORE, on the other hand, means that you can use one or more of something, but it is also okay to leave it out.

For instance, I said earlier that the ls command, along with the -l option, lists information about the files you specify. The exact format of the command requires you to specify zero or more file names. Here are three examples:

ls -l
ls -l file1
ls -l file1 file2 data1 data2

Whenever you see a specification that requires zero or more of something, you should ask, "What happens if I don't use any?" Frequently, there is a DEFAULT — an assumed value — that will be used.

With ls, the default is the set of files in your "working directory" (explained in Chapter 24). Thus, if you do not specify any file names — as in the first example — ls lists information about all the files in your working directory. When you specify one or more file names, ls displays information only about those particular files.

— hint —

Whenever you are told that you can use zero or more of something, ask, "What is the default?"

Jump to top of page

The Formal Description
of a Command: Syntax

A good approach to learning a new command is to answer the following three questions:

• What does the command do?
• How do I use the options?
• How do I use the arguments?

To learn what a command does, use the man command to look up the command in the online manual (see Chapter 9) and read the summary. If you need more information, take a look at the full description.

When you check the man page, you will see the command syntax: the exact, formal specification for using the command. Informally, you can think of the syntax as the "official" description of how to use the command.

Within Unix, command syntax follows seven rules. The first five rules are the most basic, so let's start with those. We'll cover the other two rules in a moment.

  1. Items in square brackets are optional.
  1. Items not in square brackets are obligatory.
  1. Anything in boldface must be typed exactly as written.
  1. Anything in italics must be replaced by an appropriate value.
  1. An argument followed by an ellipsis (...) may be repeated any number of times.

Here is an example to show how it all works. The following is the syntax for the ls command on one particular Unix system:

ls [-aAcCdfFgilLqrRstu1] [filename...]

From looking at the syntax, what can we say about this command?

• The command has 18 different options. You can use -a, -A, -c, -C and so on. Since the options are optional, they are enclosed in square brackets. In other words, you can use zero or more options.

• There is one argument, filename. This argument is optional, as it too is enclosed in square brackets.

• The name of the command and the options are printed in boldface. This means that they must be typed exactly as they appear.

• The argument is in italics. This means that you must replace it with an appropriate value. (In this case, the name of a file or a directory.)

• The argument is followed by "...", which means that you can use more than one argument (to specify the name of more than one file). Since the argument is itself optional, we can be precise and say that you must specify zero or more file names.

Based on this syntax, here are some valid ls commands. Remember, single-hyphen options can be typed separately or grouped together with a single hyphen:

ls -l
ls file1
ls file1 file2 file3 file4 file5
ls -Fl file1 file2
ls -F -l file1 file2

Here are some invalid ls commands. The first command is invalid because it uses an unknown option (-z):

ls -lz file1 file2

The next command is invalid because the option comes after an argument:

ls file1 -l file2

This example is tricky, and it shows why you must follow the syntax exactly.

As a general rule, options must come before arguments (although there can be exceptions, which I will mention in a moment). Since the word file1 does not begin with a hyphen, ls assumes it is an argument, as is anything else that follows. Thus, ls thinks you are specifying the names of three files: file1, -l and file2. Of course, there is no file named -l, so the results of this command will not be what you intended.

The last two syntax rules cover more complicated situations.

  1. If you see a single option grouped with an argument, the option and argument must be used together.

The following example (a simplified version of the Linux man command) illustrates this rule:

man [-P pager] [-S sectionlist] name...

In this case, if you want to use the -P option, it must be immediately followed by the argument pager. Similarly, if you want to use the -S option, it must be immediately followed by the argument sectionlist.

As you can see, this type of syntax is an exception to the general guideline that all options come before arguments. In this case, it is possible for the pager argument to come before the -S option.

Finally, here is the last syntax rule.

  1. Two or more items separated by a | (vertical bar) character, indicates that you are to choose one item from a list.

This is illustrated by the next example, which shows the syntax of the Linux version of the who command:

who [-abdHilmpqrstTu] [file | arg1 arg2]

In this case, the syntax tells us that we can use either a single argument named file or two arguments named arg1 and arg2.

Here are two examples. The first specifies file; the second specifies arg1 and arg2:

who /var/run/utmp
who am i

Don't worry about the details(*). What I want you to notice is how the vertical bar is used to specify alternatives when you have more than one choice.

* Footnote

If you read the man page for who, you will see that the first argument (file) is the location of the file from which who draws its information. You can specify your own file instead of the default. This is not something you would normally do, but the idea is straightforward.

The other two arguments (arg1 and arg2) are more interesting. You may recall from Chapter 8 that you can display the name of the userid that is currently logged in by entering:

who am i

Interestingly enough, the who command will respond in this way no matter what you type, as long as you specify two arguments. For example, you can type:

who are you
who goes there

Or, if you are brave:

who is god

Why do you think the command was programmed in this way?

Jump to top of page

Learning Command Syntax
From the Unix Manual

When you read printed material (such as this book), it is easy to see which words are in boldface and which are in italics. However, when you look at the online manual on your monitor, you may not see the special typefaces.

On some systems, you will see boldface and italics; on others, you will not. On systems that don't show italics, you will often see underlining instead. Regardless, you will have to get used to your particular system so you can deduce, from context, exactly which words are arguments. Most of the time, this is not difficult.

A typical man page will explain each possible option and argument. However, within the syntax summary, some versions of the manual use a simplified form in which the individual options are not listed. Rather, they are represented by the word "options". Here is an example. Earlier I showed the syntax for one version of the ls command:

ls [-aAcCdfFgilLqrRstu1] [filename...]

Using the simplified system, the syntax would be:

ls [options] [filename...]

Not to worry. Whether or not you see the options in the syntax summary, they will all be enumerated — and explained — within the detailed command description.

Jump to top of page

How Can You Learn So Many Options?

You will have noticed that in the example we have been using, the ls command has 18 different options. Actually, some versions of the ls command have even more options, many of which are the so-called dash-dash options. The question arises, how can you learn so many options?

The answer is you don't. Nobody remembers all the options for every command, even for the commands they use regularly. The best idea is to memorize only the most important options. When you need to use other options, look them up — that is what the online manual is for.

One of the characteristics of Unix programmers is that they tend to write programs with many options, most of which you can safely ignore. Moreover, it is not uncommon to find that different versions of Unix offer different options for the same command.

The ls command we have been using is from one particular type of Unix. Other systems will have ls commands that have a different number of options. However, the most important options — the ones you will use most of the time — rarely vary much from one system to another.

In this book, I explain many Unix commands. As I do, I will make a point to describe only the most important options and arguments, the ones you will need most of the time. If you are learning about a command, and you are curious as to all the possible options and arguments, all you have to do is take a moment to check with the manual on your system.

Consider the syntax for the man command as I described it in Chapter 9:

man [section] name...
man -f name...
man -k keyword...

Since this command can be used in three different ways, it is easiest to show its syntax using three different descriptions.

The first way to use man is with an optional section number and one or more name values. The second way is to use the -f option and one or more name values. The third way is to use the -k option and one or more keyword values.

These are not the only options that man uses, just the most important ones. On some systems, man has a large number of options. However, for most day-to-day work, -f and -k are the only ones you will need. If you want to know more about what is available on your system, just check the manual.

To conclude this chapter, here is one final example. As I explained in Chapter 9, you can use the whatis command instead of man -f, and the apropos command instead of man -k. The syntax for these two commands is:

whatis name...
apropos keyword...

The syntax shows us that, to use either of these commands, you enter the command name (whatis or apropos) followed by one or more arguments.

— hint —

Some commands have minor differences from one version of Unix to another. In this book, I generally use the GNU/Linux version of the command. Most of the time, this will be fine, as the most important options and arguments tend to be the same in most types of Unix.

However, if you have a problem, the definitive source for how a program works on your system is your online manual, not this book (or any book).

Jump to top of page



Exercises

Review Question #1:

How do you enter more than one command on the same line?

Review Question #2:

The syntax of a Unix command can be expressed in the form:

command-name options arguments

What are options? What are arguments?

Review Question #3:

What are dash options and dash-dash options?

What is each type used for?

Review Question #4:

What is whitespace?

Review Question #5:

When you learn the syntax of a new program, what are the three basic questions you should ask?

Applying Your Knowledge #1:

It is often desirable to enter more than one command on the same line. Create a short summary of your system's status by typing the following three commands on a single line:

date (time and date)
users (userids that are logged in)
uptime (how long your system has been up)

Applying Your Knowledge #2:

Write the syntax for the following program. The command name is foobar. There are three options -a, -b and -c. The -c option takes an optional argument named value. Finally, there must be one or more instances of an argument named file.

For Further Thought #1:

Many of the GNU utilities (used with Linux and FreeBSD) support both the traditional, abbreviated - (dash) options, as well as the longer -- (dash-dash) options. Why did the GNU developers feel it was necessary to introduce a new style of options?

What are the advantages? What are the disadvantages?

Jump to top of page