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
Most commands have optional flags (sometimes called options) that control the behavior of the command. By convention, flags begin with a '-' character.
The flags in the example above have the following meaning:
-a
: tells ls
to show "hidden" files
(files whose names begin with '.', which ls would not normally
list).
-l
: tells ls
to do a "long listing", which is to show lots of information
about each file and directory instead of just the name.
Single-letter flags can usually be combined, e.g.
-a -l
can be abbreviated as -al
.
Most newer Unix commands also support long flag names, mainly to
improve readability of commands used in scripts. For example,
in the Unix zip command, -C
and --preserve-case
are synonymous. Using -C
saves typing, but --preserve-case is more easily understood.
/etc
and
/var
arguments above are directories
to be listed by ls. If no data arguments are
given to ls, it lists the current
working directory (described in Section 1.8, “Processes”).
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 programmer writing 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. The ls command will look for two separate files or 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
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 shell-prompt: rmdir My shell-prompt: rmdir Programs shell-prompt: mkdir 'My Programs' shell-prompt: ls shell-prompt: ls My Programs shell-prompt: ls "My Programs"
Make sure you are using the latest version of this document.
Carefully read one section of this document and casually read other material (such as corresponding sections in a textbook, if one exists) if needed.
Try to answer the questions from that section. If you do not remember the answer, review the section to find it.
Write the answer in your own words. Do not copy and paste. Verbalizing answers in your own words helps your memory and understanding. Copying does not, and demonstrates a lack of interest in learning.
Check the answer key to make sure your answer is correct and complete.
DO NOT LOOK AT THE ANSWER KEY BEFORE ANSWERING QUESTIONS TO THE VERY BEST OF YOUR ABILITY. In doing so, you would only cheat yourself out of an opportunity to learn and prepare for the quizzes and exams.
Important notes:
Show all your work. This will improve your understanding and ensure full credit for the homework.
The practice problems are designed to make you think about the topic, starting from basic concepts and progressing through real problem solving.
Try to verify your own results. In the working world, no one will be checking your work. It will be entirely up to you to ensure that it is done right the first time.
Start as early as possible to get your mind chewing on the questions, and do a little at a time. Using this approach, many answers will come to you seemingly without effort, while you're showering, walking the dog, etc.
What are the three major components of a Unix command?
What are the two sources of the command name?
How do we know whether an argument is a flag or data?
What is the advantage of short flags and the advantage of long flags?
What do data argument represent?
What rules does Unix enforce regarding the order of arguments?
What separates one Unix argument from the next?
Can an argument contain whitespace? If so, how?