BUY THIS BOOK

Safari Books Online

What is this?

Looking to Reprint this content?


UNIX in a Nutshell
UNIX in a Nutshell, Third Edition

By Arnold Robbins

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Introduction
The Unix operating system originated at AT&T Bell Labs in the early 1970s. System V Release 4 came from USL (Unix System Laboratories) in the late 1980s. Unix source code is currently owned by SCO (the Santa Cruz Operation). Because Unix was able to run on different hardware from different vendors, developers were encouraged to modify Unix and distribute it as their own value-added version. Separate Unix traditions evolved as a result: USL's System V, Berkeley Software Distribution (BSD, from the University of California, Berkeley), Xenix, etc.
Today, Unix developers have blended the different traditions into a more standard version. (The ongoing work on POSIX, an international standard based on System V and BSD, is influencing this movement.) This quick reference describes two systems that offer what many people consider to be a “more standard” version of Unix: System V Release 4 (SVR4) and Solaris 7.
SVR4, which was developed jointly by USL (then a division of AT&T) and Sun Microsystems, merged features from BSD and SVR3. This added about two dozen BSD commands (plus some new SVR4 commands) to the basic Unix command set. In addition, SVR4 provides a BSD Compatibility Package, a kind of “second string” command group. This package includes some of the most fundamental BSD commands, and its purpose is to help users of BSD-derived systems make the transition to SVR4.
Solaris 7 is a distributed computing environment from Sun Microsystems. The history of Solaris 7 is more complicated.
Solaris 7 includes the SunOS 5.7 operating system, plus additional features such as the Common Desktop Environment and Java tools. SunOS 5.7, in turn, merges SunOS 4.1 and SVR4. In addition, the kernel has received significant enhancement to support multiprocessor CPUs, multithreaded processes, kernel-level threads, and dynamic loading of device drivers and other kernel modules. Most of the user-level (and system administration) content comes from SVR4. As a result, Solaris 7 is based on SVR4 but contains additional BSD/SunOS features. To help in the transition from the old (largely BSD-based) SunOS, Solaris provides the BSD/SunOS Compatibility Package and the Binary Compatibility Package.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Merging the Traditions
Today, Unix developers have blended the different traditions into a more standard version. (The ongoing work on POSIX, an international standard based on System V and BSD, is influencing this movement.) This quick reference describes two systems that offer what many people consider to be a “more standard” version of Unix: System V Release 4 (SVR4) and Solaris 7.
SVR4, which was developed jointly by USL (then a division of AT&T) and Sun Microsystems, merged features from BSD and SVR3. This added about two dozen BSD commands (plus some new SVR4 commands) to the basic Unix command set. In addition, SVR4 provides a BSD Compatibility Package, a kind of “second string” command group. This package includes some of the most fundamental BSD commands, and its purpose is to help users of BSD-derived systems make the transition to SVR4.
Solaris 7 is a distributed computing environment from Sun Microsystems. The history of Solaris 7 is more complicated.
Solaris 7 includes the SunOS 5.7 operating system, plus additional features such as the Common Desktop Environment and Java tools. SunOS 5.7, in turn, merges SunOS 4.1 and SVR4. In addition, the kernel has received significant enhancement to support multiprocessor CPUs, multithreaded processes, kernel-level threads, and dynamic loading of device drivers and other kernel modules. Most of the user-level (and system administration) content comes from SVR4. As a result, Solaris 7 is based on SVR4 but contains additional BSD/SunOS features. To help in the transition from the old (largely BSD-based) SunOS, Solaris provides the BSD/SunOS Compatibility Package and the Binary Compatibility Package.
Sun has made binary versions of Solaris for the SPARC and Intel architectures available for “free,” for noncommercial use. You pay only for the media, shipping, and handling. To find out more, see http://www.sun.com/developer.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Bundling
Another issue affecting Unix systems is the idea of bundling. Unix has many features—sometimes more than you need to use. Nowadays, Unix systems are often split, or bundled, into various component packages. Some components are included automatically in the system you buy; others are optional; you get them only if you pay extra. Bundling allows you to select only the components you need. Typical bundling includes the following:
Basic system
Basic commands and utilities
Programming
Compilers, debuggers, and libraries
Text processing
troff, macros, and related tools
Windowing
Graphical user interfaces such as OPEN LOOK, Motif, and CDE—the Common Desktop Environment
Bundling depends on the vendor. For example, Solaris comes with text-processing tools. For others, they are an extra-cost option. Similarly, some vendors ship compilers, and others don't.
When you (or your system administrator) first install Solaris, you have the choice of three levels of installation:
End User System Support
This is the simplest system.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
What's in the Quick Reference
This guide presents the major features of generic SVR4, plus a few extras from the compatibility packages and from Solaris 7. In addition, this guide presents chapters on emacs and RCS. Although they are not part of the standard SVR4 distribution, they are found on many Unix systems because they are useful add-ons.
But keep in mind: if your system doesn't include all the component packages, there will be commands in this book you won't find on your system.
The summary of Unix commands in Chapter 2, makes up a large part of this book. Only user/programmer commands are included; administrative commands are ignored. Chapter 2 describes the following set:
  • Commands and options in SVR4
  • Selected commands from the compatibility packages and from Solaris 7, such as the Java-related tools
  • “Essential” tools for which source and/or binaries are available via the Internet
Solaris users should note that the following commands are either unbundled or unavailable:
cb cc cflow cof2elf
cscope ctrace cxref lprof
pic
Appendix B, describes SVR4 commands that are obsolete. These commands still ship with SVR4 or Solaris, but their functionality has been superseded by other commands or technologies.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Beginner's Guide
If you're just beginning to work on a Unix system, the abundance of commands might prove daunting. To help orient you, the following lists present a small sampling of commands on various topics.
ftp File transfer protocol.
login Sign on to Unix.
mailx Read or send mail.
rlogin Sign on to remote Unix.
talk Write to other terminals.
telnet Connect to another system.
vacation Respond to mail automatically.
cmp Compare two files, byte by byte.
comm Compare items in two sorted files.
diff Compare two files, line by line.
diff3 Compare three files.
dircmp Compare directories.
sdiff Compare two files, side by side.
cat Concatenate files or display them.
cd Change directory.
chmod Change access modes on files.
cp
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Guide for Users of BSD-Derived Systems
Those of you making a transition to SVR4 from a BSD-derived system should note that BSD commands reside in your system's /usr/ucb directory. This is especially important when using certain commands, because the compatibility packages include several commands that have an existing counterpart in SVR4, and the two versions usually work differently. If your PATH variable specifies /usr/ucb before the SVR4 command directories (e.g., /usr/bin), you'll end up running the BSD version of the command. Check your PATH variable (use echo $PATH) to make sure you get what you want. The commands that have both BSD and SVR4 variants are:
basename du ls tr
cc echo ps vacation
chown groups stty
deroff ld sum
df ln test
This book describes the SVR4 version of these commands. (Often, the standard Solaris version of a command includes features or options from the BSD version as well.)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Solaris: Standard Compliant Programs
There are a number of different standards that specify the behavior of portable programs in a Unix-like environment. POSIX 1003.2 and XPG4 are two of the more widely known ones. Where the behavior specified by a standard differs from the historical behavior provided by a command, Solaris provides a different version of the command in /usr/xpg4/bin. These commands are listed here, but not otherwise covered in this book, as most users typically do not have /usr/xpg4/bin in their search paths. The manual entries for each command discuss the differences between the /usr/bin version and the /usr/xpg4/bin version.
ar ed make rm
awk edit more sccs
basename env nice sed
cp expr nl sort
ctags get nm stty
date grep nohup tail
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Unix Commands
This chapter presents the Unix commands of interest to users and programmers. Most of these commands appear in the “Commands” section of the User's Reference Manual and Programmer's Reference Manual for Unix System V Release 4 (SVR4). This chapter describes additional commands from the compatibility packages; these commands are prefixed with /usr/ucb, the name of the directory in which they reside. Also included here are commands specific to Solaris 7, such as those for using Java and the occasional absolutely essential program available from the Internet.
Particularly on Solaris, useful commands are spread across a number of different “bin” directories, such as /usr/ccs/bin, /usr/dt/bin, /usr/java/bin, and /usr/openwin/bin, and not just /usr/bin and /usr/ucb. In such cases, this book provides the full pathname, e.g., /usr/ccs/bin/make. In some instances, a symbolic link for a command exists in /usr/bin to the actual command elsewhere.
Each entry is labeled with the command name on the outer edge of the page. The syntax line is followed by a brief description and a list of all available options. Many commands come with examples at the end of the entry. If you need only a quick reminder or suggestion about a command, you can skip directly to the examples.
Note: comments such as "SVR4 only," or "Solaris only," compare only those two systems. Many "Solaris only" commands and/or options are commonly available on other Unix systems as well.
Some options can be invoked only by a user with special system privileges. Such a person is often called a “superuser.” This book uses the term privileged user instead.
Typographic conventions for describing command syntax are listed in the Preface. For additional help in locating commands, see the Index.
addbib
addbib
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Alphabetical Summary of Commands
addbib
addbib [options] database
Description
Part of the refer suite of programs. See Chapter 17.
admin
/usr/ccs/bin/admin [options] files
Description
An SCCS command. See Chapter 18.
appletviewer
/usr/java/bin/appletviewer [options] urls
Description
Solaris only. Connect to the specified urls and run any Java applets they specify in their own windows, outside the context of a web browser.
Options
-debug
Run the applet viewer from within the Java debugger, jdb.
-encoding name
Specify the input HTML file encoding.
-J opt
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: The Unix Shell: An Overview
For novice users, this chapter presents basic concepts about the Unix shell. For advanced users, this chapter also summarizes the major similarities and differences between the Bourne, Korn, and C shells. Details on the three shells are provided in Chapter 4, and Chapter 5.
The following topics are presented:
  • Introduction to the shell
  • Purpose of the shell
  • Shell flavors
  • Common features
  • Differing features
Let's suppose that the Unix operating system is a car. When you drive, you issue a variety of “commands”: you turn the steering wheel, press the accelerator, or press the brake. But how does the car translate your commands into the action you want? The car's drive mechanism, which can be thought of as the car's user interface, is responsible. Cars can be equipped with front-wheel drive, rear-wheel drive, four-wheel drive, and sometimes combinations of these.
The shell is the user interface to Unix, and by the same token, several shells are available in Unix. Most systems provide more than one for you to choose from. Each shell has different features, but all of them affect how commands will be interpreted and provide tools to create your Unix environment.
The shell is simply a program that allows the system to understand your commands. (That's why the shell is often called a command interpreter.) For many users, the shell works invisibly—“behind the scenes.” Your only concern is that the system does what you tell it to do; you don't care about the inner workings. In our car analogy, this is comparable to pressing the brake. Most of us don't care whether the user interface involves disk brakes or drum brakes, as long as the car stops.
There are three uses for the shell:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction to the Shell
Let's suppose that the Unix operating system is a car. When you drive, you issue a variety of “commands”: you turn the steering wheel, press the accelerator, or press the brake. But how does the car translate your commands into the action you want? The car's drive mechanism, which can be thought of as the car's user interface, is responsible. Cars can be equipped with front-wheel drive, rear-wheel drive, four-wheel drive, and sometimes combinations of these.
The shell is the user interface to Unix, and by the same token, several shells are available in Unix. Most systems provide more than one for you to choose from. Each shell has different features, but all of them affect how commands will be interpreted and provide tools to create your Unix environment.
The shell is simply a program that allows the system to understand your commands. (That's why the shell is often called a command interpreter.) For many users, the shell works invisibly—“behind the scenes.” Your only concern is that the system does what you tell it to do; you don't care about the inner workings. In our car analogy, this is comparable to pressing the brake. Most of us don't care whether the user interface involves disk brakes or drum brakes, as long as the car stops.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Purpose of the Shell
There are three uses for the shell:
  • Interactive use
  • Customization of your Unix session
  • Programming
When the shell is used interactively, the system waits for you to type a command at the Unix prompt. Your commands can include special symbols that let you abbreviate filenames or redirect input and output.
A Unix shell defines variables to control the behavior of your Unix session. Setting these variables will tell the system, for example, which directory to use as your home directory, or the file in which to store your mail. Some variables are preset by the system; you can define others in startup files that are read when you log in. Startup files can also contain Unix commands or special shell commands. These are executed every time you log in.
Unix shells provide a set of special (or built-in) commands that let you create programs called shell scripts. In fact, many built-in commands can be used interactively like Unix commands, and Unix commands are frequently used in shell scripts. Scripts are useful for executing a series of individual commands. This is similar to BATCH files in MS-DOS. Scripts can also execute commands repeatedly (in a loop) or conditionally (if-else), as in many high-level programming languages.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Shell Flavors
Many different Unix shells are available. This quick reference describes the three most popular shells:
  • The Bourne (or standard) shell, the most compact shell and also the simplest.
  • The Korn shell, a superset of the Bourne shell that lets you edit the command line. There are in fact two commonly available versions of the Korn shell, distinguished by the year they were released, and referred to in this book as ksh88 and ksh93 respectively.
  • The C shell, which uses C-like syntax and is more convenient for the interactive user than the Bourne shell.
Most systems have more than one shell, and people will often use the Bourne shell for writing shell scripts and another shell for interactive use.
The /etc/passwd file determines which shell takes effect during your interactive Unix session. When you log in, the system checks your entry in /etc/passwd. The last field of each entry names a program to run as the default shell. For example:
If the program name is: Your shell is the:
/bin/sh Bourne shell
/bin/rsh Restricted Bourne shell
/bin/jsh Bourne shell, including job control
/bin/ksh Korn shell
/usr/dt/bin/dtksh
The Desktop Korn shell, a version of ksh93 (Solaris only)
/bin/rksh
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Common Features
The following table displays features that are common to the Bourne, Korn, and C shells. Note that the Korn shell is an enhanced version of the Bourne shell; therefore, the Korn shell includes all features of the Bourne shell, plus some others. The commands bg, fg, jobs, stop, and suspend are available only on systems that support job control. (Essentially all modern Unix systems do.)
Symbol/Command Meaning/Action
> Redirect output.
>> Append to file.
< Redirect input.
<< “Here” document (redirect input).
| Pipe output.
|& Start a coprocess. Korn shell only.
& Run process in background.
; Separate commands on same line.
* Match any character(s) in filename.
? Match single character in filename.
[ ] Match any characters enclosed.
( ) Execute in subshell.
‘ ‘ Substitute output of enclosed command.
" " Partial quote (allows variable and command expansion).
' ' Full quote (no expansion).
\ Quote following character.
$ var
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Differing Features
The following table displays features that are different among the three shells.
sh ksh csh Meaning/Action
$ $ % Prompt.
>| >! Force redirection.
>>! Force append.
> file 2>&1 > file 2>&1 >& file Combine stdout and stderr.
{ } Expand elements in list.
‘ ‘ ‘ ‘ ‘ ‘
Substitute output of enclosed command.
$( )
Substitute output of enclosed command. (Preferred form.)
$HOME $HOME $home Home directory.
~ ~ Home directory symbol.
var = value
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: The Bourne Shell and Korn Shell
This chapter presents the following topics:
  • Overview of features
  • Syntax
  • Variables
  • Arithmetic expressions (Korn shell only)
  • Command history (Korn shell only)
  • Job control
  • Invoking the shell
  • Restricted shells
  • Built-in commands
http://www.kornshell.com provides considerable information about the Korn shell. Follow the links there for binaries of ksh93 that can be downloaded for noncommercial and educational use. See also Learning the Korn Shell, which is listed in the Bibliography.
The Bourne shell is the standard shell and provides the following features:
  • Input/output redirection
  • Wildcard characters (metacharacters) for filename abbreviation
  • Shell variables for customizing your environment
  • A built-in command set for writing shell programs
  • Job control (beginning in SVR4)
The Korn shell is a backward-compatible extension of the Bourne shell. Features that are valid only in the Korn shell are so indicated:
  • Command-line editing (using the command syntax of either vi or emacs)
  • Access to previous commands (command history)
  • Integer arithmetic
  • More ways to match patterns and substitute variables
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Overview of Features
The Bourne shell is the standard shell and provides the following features:
  • Input/output redirection
  • Wildcard characters (metacharacters) for filename abbreviation
  • Shell variables for customizing your environment
  • A built-in command set for writing shell programs
  • Job control (beginning in SVR4)
The Korn shell is a backward-compatible extension of the Bourne shell. Features that are valid only in the Korn shell are so indicated:
  • Command-line editing (using the command syntax of either vi or emacs)
  • Access to previous commands (command history)
  • Integer arithmetic
  • More ways to match patterns and substitute variables
  • Arrays and arithmetic expressions
  • Command-name abbreviation (aliasing)
  • More built-in commands
ksh93 adds the following capabilities:
  • Upwards compliance with POSIX
  • Internationalization facilities
  • An arithmetic for loop
  • Floating-point arithmetic and built-in arithmetic functions
  • Structured variable names and indirect variable references
  • Associative arrays
  • Even more ways to match patterns and substitute variables
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Syntax
This section describes the many symbols peculiar to the Bourne and Korn shells. The topics are arranged as follows:
  • Special files
  • Filename metacharacters
  • Quoting
  • Command forms
  • Redirection forms
  • Coprocesses (Korn shell only)
/etc/profile
Executed automatically at login, first.
$HOME/.profile
Executed automatically at login, second.
$ENV
Specifies the name of a file to read when a new Korn shell is created. (ksh88: all shells. ksh93: interactive shells only.) The value is variable (ksh93: and command and arithmetic) substituted in order to determine the actual file name. Login shells read $ENV after processing /etc/profile and $HOME/.profile.
/etc/passwd
Source of home directories for ~ name abbreviations. (On networked systems, this information may come from NIS or NIS+, not your workstation password file.)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Variables
This section describes the following:
  • Variable substitution
  • Built-in shell variables
  • Other shell variables
  • Arrays (Korn shell only)
  • Discipline functions (ksh93 only)
ksh93 provides structured variables, such as pos.x and pos.y. To create either one, pos must already exist, and braces must be used to retrieve their values. Names beginning with .sh are reserved for use by ksh.
No spaces should be used in the following expressions. The colon (:) is optional; if it's included, var must be nonnull as well as set.
var = value ...
Set each variable var to a value.
${ var }
Use value of var; braces are optional if var is separated from the following text. They are required in ksh93 if a variable name contains periods.
${ var :- value }
Use var if set; otherwise, use value.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Arithmetic Expressions
The Korn shell's let command performs arithmetic. ksh88 is restricted to integer arithmetic. ksh93 can do floating-point arithmetic as well. The Korn shell provides a way to substitute arithmetic values (for use as command arguments or in variables); base conversion is also possible:
$(( expr ))
Use the value of the enclosed arithmetic expression.
B # n
Interpret integer n in numeric base B. For example, 8#100 specifies the octal equivalent of decimal 64.
The Korn shell uses arithmetic operators from the C programming language; in decreasing order of precedence.
Operator
Description
++ --
Auto-increment and auto-decrement, both prefix and postfix. ksh93 only.
+
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Command History
The Korn shell lets you display or modify previous commands. Commands in the history list can be modified using:
  • Line-edit mode
  • The fc and hist commands
Line-edit mode emulates many features of the vi and emacs editors. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. You can also change the line before executing it. When you're ready to issue the command, press the Return key.
Line-edit mode can be started in several ways. For example, these are equivalent:
$ VISUAL=vi
$ EDITOR=vi
$ set -o vi	
                  Overrides value of VISUAL or EDITOR
               
Note that vi starts in input mode; to type a vi command, press the Escape key first.

Section 4.5.1.1: Common editing keystrokes

vi
emacs
Result
k
CTRL-p
Get previous command.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Job Control
Job control lets you place foreground jobs in the background, bring background jobs to the foreground, or suspend (temporarily stop) running jobs. Job control is enabled by any of the following commands:
jsh -i		Bourne shell

ksh -m -i		Korn shell (same as next two)
set -m
set -o monitor
Many job control commands take a jobID as an argument. This argument can be specified as follows:
% n
Job number n.
% s
Job whose command line starts with string s.
%? s
Job whose command line contains string s.
%%
Current job.
%+
Current job (same as above).
%-
Previous job.
The Bourne and Korn shells provide the following job control commands. For more information on these commands, see Section 4.9 later in this chapter.
bg
Put a job in the background.
fg
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Invoking the Shell
The command interpreter for the Bourne shell (sh) or the Korn shell (ksh) can be invoked as follows:
sh   [options]  [arguments]

ksh  [options]  [arguments]
ksh and sh can execute commands from a terminal, from a file (when the first argument is an executable script), or from standard input (if no arguments remain or if -s is specified). ksh and sh automatically print prompts if standard input is a terminal, or if -i is given on the command line.
Arguments
Arguments are assigned in order to the positional parameters $1, $2, etc. If array assignment is in effect (-A or +A), arguments are assigned as array elements. If the first argument is an executable script, commands are read from it, and the remaining arguments are assigned to $1, $2, etc.
Options
-c str
Read commands from string str.
-D
Print all $"..." strings in the program. ksh93 only.
-i
Create an interactive shell (prompt for input).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Restricted Shells
Restricted shells can be invoked in any of the following ways:
rksh		Korn shell
ksh -r
set -r

/usr/lib/rsh	Bourne shell
set -r
Restricted shells can also be set up by supplying the full pathname to rksh or rsh in the shell field of /etc/passwd or by using them as the value for the SHELL variable.
Restricted shells act the same as their nonrestricted counterparts, except that the following are prohibited:
  • Changing directory (i.e., using cd).
  • Setting the PATH variable. rksh also prohibits setting ENV and SHELL.
  • Specifying a / for command names or pathnames.
  • Redirecting output (i.e., using > and >>). ksh also prohibits the use of <>.
  • Adding new built-in commands (ksh93).
Shell scripts can still be run, since in that case the restricted shell calls ksh or sh to run the script. This includes the /etc/profile, $HOME/.profile, and $ENV files.
Restricted shells are not used much in practice, as they are difficult to set up correctly.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Commands (Bourne and Korn Shells)
Content preview·