Memra

Anatomy of a UNIX command

◈ 7 cards

Split any command line into command, options, option arguments and command arguments, and apply the seven syntax rules that govern it.

The generic form

Every UNIX command line has the same four-part shape, and three of the four parts are optional:

command [-option(s)] [option argument(s)] [command argument(s)]

Only the command is always present. whoami and pwd are complete command lines with nothing else at all.

Seven rules govern how those parts are written, and they are the rules an exam question about a command line is testing:

  1. Whitespace separates the parts. One space is enough; more is harmless.
  2. Short options take one hyphen; long options take two. Never a space after the hyphen(s): --help, not -- help.
  3. Short options that take no argument may be bundled into a single token: -l -a -h may be written -lah.
  4. A space between an option and its option argument is optional. -d: and -d : mean the same thing.
  5. Order among options does not matter. ls -l -a and ls -a -l are the same command.
  6. Some commands take nothing. whoami accepts no options and no arguments.
  7. Everything is case sensitive — commands, options, filenames, usernames, all of it.

Worked example: cut -d: -f 1 /etc/passwd

This line prints the first colon-separated field of every line of the account file — that is, every username on the machine. Take it token by token.

cut is the command. It is the program the shell will look for on your PATH, and it is always the first token.

-d: is one token doing two jobs. The hyphen marks d as an option — it sets the field delimiter. The : immediately after it is that option's option argument. There is no space, and by rule 4 there did not need to be one; -d : would have been identical. This is the token most people mis-parse, so say it out loud: option d, option argument colon.

-f is a second option — select fields — and it also needs an argument.

1 is that argument: an option argument belonging to -f. Notice that it is a bare 1 with no hyphen, and notice what it is not: it is not a command argument, because cut does not operate on the number 1. It qualifies -f.

/etc/passwd is a command argument. No hyphen, and it names the thing the command actually operates on.

That gives the full parse: command cut; options d and f; option arguments : and 1; command argument /etc/passwd.

Two things that go wrong

First, the missing space. Type ls-la and the shell does not see ls with options. It sees one word, ls-la, looks for a program of that name on your PATH, fails to find one, and reports ls-la: command not found. The shell splits your line on whitespace, and on nothing else. It does not know that ls is a command and -la is not.

Second, the argument that looks like an option and is not. In chmod g+rwx report.txt there are no options at all. g+rwx has no leading hyphen; it is a command argument — the symbolic mode chmod operates with — sitting alongside the second command argument report.txt. Parsing it as an option is the classic error, and the giveaway is always the same: is there a hyphen?

When it runs and does the wrong thing

A useful rule of thumb: no error message and a fresh prompt means the command ran. It emphatically does not mean it did what you wanted. A syntactically perfect line with the wrong option runs perfectly and silently produces the wrong answer — and typos account for the overwhelming majority of beginner failures. Read your line back before you press Enter, especially when it is destructive.

tokenpartwhycutcommandthe program to run; alwaysthe first token-d:option + option argumenthyphen marks option d; the: is its argument, attached-foptionselects fields; needs anargument of its own1option argumentqualifies -f, not cut/etc/passwdcommand argumentno hyphen; it is what thecommand operates on-d: and -d : are the same thing
The parse is mechanical once you ask two questions of each token: does it have a leading hyphen, and does the option before it need an argument?
NORMAL ~/memra/learn/comp-325/anatomy-of-a-unix-command utf-8 LF