Learning the Korn Shell, 2nd Edition
Arnold Robbins
Bill Rosenblatt
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Dedication
To Margot Lorraine Rosenblatt, born April 16, 2001.
Bill Rosenblatt
To my wife, Miriam, for your love and support. To my children, Chana, Rivka, Nachum, and Malka.
Arnold Robbins
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596001957/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
The long, tortuous history of the Unix operating system has resultedin systems with all kinds of permutations and combinations of features.This means that whenever you walk up to an unfamiliar Unix system, youneed to find out certain things about it in order to use it properly.And even on a given system, you may have a number of choices you canmake about what features you want to use.
The most important such decision if you get to make it is what shell to use. Shell is Unix jargon for the program thatallows you to communicate with the computer by entering commands andgetting responses. The shell is completely separate from theUnix operating system per se; its just a program that runson Unix.With other systemssuch as MS-DOS, Microsoft Windows, Macintosh,[]OpenVMS, and VM/CMS,the command interpreter or user interface is an integral part of theoperating system.
Nowadays there are dozens of different shells floating around,ranging from the original standard, the Bourne shell, to menu-basedand graphical interfaces. The most important shells have been theBourne shell, the C shell, and the Korn shell thesubject of this book.
Korn Shell Versions
.
The 1993 version is now available in both source code form and as precompiled executablesfor many common systems.Downloading and building it is described in.The latest, downloadable version of ksh has a number of featuresnot in earlier versions. We cover all of those features, too.We have made an effort to point out when something may not be in anearlier version, but caveat emptor; if you need a feature and yoursystems version of the 1993 Korn shell doesnt have it, you mayneed to download a prebuilt executable or download the source and build your own executable.
.
Summary of Korn Shell Features
The Korn shell is the most advanced of the shells that are officiallydistributed with Unix systems.Its a backward-compatible evolutionarysuccessor to the Bourne shell that includes most of the C shellsmajor advantages as well as a number of new features of its own.
Features appropriated from the C shell include:
Job control
The ability to stop jobs with CTRL-Z and move them to the foreground orbackground withthe fg and bg commands.
Aliases
The ability to define shorthand names for commandsor command lines.
Functions
The ability tostore your own shell code inmemory instead of files.Functions increase programmability and efficiency.(Functions have been common in the Bourne shell for many years.)
Command history
The ability to recall previously entered commands.
The Korn shells major new features include:
Command-line editing
This feature allows youto use vi orEmacs-style editing commands on your command lines.
Integrated programming features
The functionality ofseveral external Unix commands, including test , expr , getopt , and echo , has been integrated into theshell itself, enabling common programming tasks to be donemore cleanly and without creating extra processes.
Control structures
Additional flow-control structures,especially the select
construct,enable easy menu generation.
Debugging primitives
These features make it possible to writetools that help programmers debug their shell code.
Regular expressions
Well known to users of Unix utilitieslike grep and awk ,regular expressions (albeit with a different syntax)have been added to the standard setof filename wildcards and to the shell variable facility.
Advanced I/O features
Several new facilities for control of process I/O,including the ability to dotwo-way communication with concurrent processes (coroutines),and to connect to network services.
New options and variables
These options and variables give you more ways to customize your environmentthan the standard Unix shells do.
Increased speed
The Korn shell often executes the same shell program considerably fasterthan the Bourne shell does.
Security features
Features designed tohelp protect against Trojan horsesand other types of break-in schemes.
Major new features in the 1993 version include:
POSIX compliance
Compliance with POSIX, an international standard for portable shellprogramming, makes it possible towrite and use portable shell scripts.
Arithmetic for loops
This new control structurelets you programmore naturally when looping a fixed number of times.
Floating-point arithmetic
The ability to use floating-point numbersand new built-in arithmetic functionsenrich the shell as a programming language.
Structured variable names
New syntax for variable namesprovides facilities similarto C structures and Ada records for grouping related items together in a variable.
Indirect variable references
This facility eases shell functionprogramming for manipulating global variables.
Associative arrays
A powerful data-management facility that is similar tothose in awk or perl .
Additional text manipulation facilities
There are even moreways to match patterns and substitute variables.
More built-in commands
Additional commandsimprove efficiency and increase script portability.
Intended Audience
This book is designed to appeal most to casual Unixusers who are just above the raw beginner level. You shouldbe familiar with the process of logging in, entering commands,and doing simple things with files.Although reviewsconcepts such as the tree-like file and directoryscheme, you may find that it moves too quickly if youre acomplete neophyte. In that case, we recommend the bookLearning the Unix Operating System byJerry Peek, Grace Todino, and John Strang, published byOReilly & Associates, Inc.
If youre an experienced user, you may wishto skip reveals a few subtle differencesbetween the Korn and C shells.