Home > Store

The Korn Shell: Unix & Linux Programming Manual, 3rd Edition

Register your product to gain access to bonus material or receive a coupon.

The Korn Shell: Unix & Linux Programming Manual, 3rd Edition


  • Sorry, this book is no longer in print.
Not for Sale


  • Copyright 2001
  • Dimensions: 7-3/8x9-1/4
  • Pages: 480
  • Edition: 3rd
  • Book
  • ISBN-10: 0-201-67523-4
  • ISBN-13: 978-0-201-67523-8

If you are a Unix or Linux Shell programmer, this book will provide you with the practical advice and technical tips you will need in order to become proficient in all aspects of the Korn Shell and enhance your programming skills. From basic introductory concepts through to advanced programming techniques, you will learn how to:

  • Customize your Unix and Linux environments
  • Write and debug Korn Shell scripts
  • Fine-tune Korn Shell scripts for faster execution

Illustrated throughout with expansive sample programs and easy-to-apply examples, plus complete ready-to-run scripts, this book will prove an indispensable guide and technical reference for the Korn Shell.

  • NEW! Chapter on pdksh, the public domain Korn Shell for Linux
  • NEW! Appendices of Pdksh quick reference and Man Page
  • NEW! CD containing pdksh source code and evaluation version of U/WIN commands.

"This is the best Korn Shell book I've seen. I presently have at least five different books on Shell programming in Unix. I actually was able to read this book cover to cover AND am able to use this as a reference...I found this book to be very complete."
John A. Siegel, Solution Engineer, Pershing, a Division of Donaldson Lufkin and Jenrette


Sample Content

Online Sample Chapter

Variables and Parameters in the Korn Shell

Table of Contents


Korn Shell 93: The Latest Version.




Cource Code Listing.

Licensing Agreement.

1. Introduction.

Major Features.

Where to Get the Korn Shell.

Which Version Do You Have?

Logging In.

Changing the Login Shell.

Invoking the Korn Shell Separately.

Using the Korn Shell in Scripts.

2. Korn Shell Basics.

Simple Script.

Process Execution.

Multiple Commands.

Continuing Commands.

Background Jobs.

Pipes Conditional Execution.

Grouping Commands.

Input/Output Redirection.

Redirecting Standard Output.

The noclobber Option.

Redirecting Standard Input.

File Descriptors.

Redirecting Standard Error.

More with File Descriptors.

Here Documents.

Here Documents and File Descriptors.

Discarding Standard Output.

File Name Substitution.

The * Character.

The ? Character.

The [ ] Characters.

The ! Character.

Matching Files.

Complex Patterns. *(pattern).

?( pattern).

+( pattern).


!( pattern).

More Complex Patterns.

Disabling File Name Substitution.

Command Substitution.

Bourne Shell Compatibility.

Directing File Input.

Arithmetic Operations.

Tilde Substitution.

3. Variables and Parameters.


Accessing Variable Values.

Variable Attributes.

Lowercase (ål) and Uppercase (åu). Attributes.

Readonly (år) Attribute.

Integer (åi) Attribute 50.

Float (åE,åF) Attribute.

Right (år) and Left (ål) Justify Attributes.

Autoexport (åx) Attribute.

Removing Variable Attributes.

Multiple Attributes.

Checking Variable Attributes.

More with Variables.

Unsetting Variables.

Special Parameters.

The ? Parameter.

The $ Parameter.

Other Special Parameters.

Special Reserved Variables.

Variable Expansion.

$variable, ${variable}. ${#variable}. ${variable:åword}, ${variableåword}. ${variable:=word}, ${variable=word}. ${variable:?word}, ${variable:?}, ${variable?word}, ${variable?}. ${variable:+word}, ${variable+word}. ${variable#pattern}, ${variable##pattern}. ${variable%pattern}, ${variable%%pattern}. ${variable//pattern1/pattern2}. ${variable/pattern1/pattern2}. ${variable#pattern1/pattern2}. ${variable/%pattern1/pattern2}. ${variable:start}, ${variable:start:length}. Array Variables. Array Variable Assignments and Declarations. Array Variable Expansion. Array Variable Attributes. Array Variable Reassignments. Associative Arrays. Compound Variables 80. Quoting. Single Quotes. Double Quotes. Back Quotes.

4. Editing Commands.

Terminal Requirements.

Command History File.

The fc Command.

Displaying the Command History File.

Editing the Command History File.

In-Line Editor.

Vi Edit Mode.

Input Mode.

Command Mode.

Moving Around the History File.

Editing Previous Commands.

Displaying Long Command Lines.

Emacs/Gmacs Edit Modes.

Editing Commands in Emacs/Gmacs Mode 100.

5. Job Control.

Manipulating Jobs.

Checking Job Status.

Killing Jobs.

Waiting for Jobs.

Background Jobs and I/O.

Job Names.

Leaving Stopped Jobs.

6. Performing Arithmetic.

The let Command.

The ((...)) Command.

Declaring Integer Variables.

Arithmetic Constants.

Arithmetic Operators.

åexpression (Unary Minus).

!expression (Logical Negation).

~expression (Bitwise Negation).

expression1, expression2 (Multiplication), expression1 *= expression2 (Multiply Assign).

expression1 / expression2 (Division), expression1 /= expression2 (Divide Assign). expression1 % expression2 (Modulo), expression1 % expression2 (Modulo Assign). expression1 + expression2 (Addition), expression1 += expression2 (Increment). expression1 å expression2 (Subtraction), expression1 å= expression2 (Decrement). identifier=expression (Assignment). expression1 << expression2 (Left Shift), expression1 <<= expression2 (Left Shift Assign). expression1 >> expression2 (Right Shift), expression1 >>= expression2 (Right Shift Assign). expression1 <= expression2 (Less Than Or Equal). expression1 < expression2 (Less Than). expression1 >= expression2 (Greater Than Or Equal). expression1 > expression2 (Greater Than). expression1 == expression2 (Equal To). expression1 != expression2 (Not Equal To). expression1 & expression2 (Bitwise AND), expression1 &= expression2 (Bitwise AND Assign). expression1 ^ expression2 (Bitwise XOR), expression1 ^= expression2 (Bitwise XOR Assign).

expression1 | expression2 (Bitwise OR), expression1 |= expression2 (Bitwise OR Assign). expression1 && expression2 (Logical AND). expression1 | | expression2 (Logical OR). (expression) (Override Precedence). Random Numbers.

7 The Environment.

After You Log In.

The Environment File.

Environment Variables.

The cd Command.




New Mail Notification Message.


Korn Shell Options.

Enabling/Disabling Options.

The ignoreeof Option.

The markdirs Option.

The noclobber Option.

The nounset Option.

Displaying the Current Settings.

Command-line Options. Aliases.

Displaying Current Aliases.

Tracked Aliases.

Removing Aliases.


Customizing Your Command Prompt.


Restricted Shell.

Privileged Mode.

8. Writing Korn Shell Scripts.

Executing Korn Shell Scripts.

Positional Parameters.

Modifying Positional Parameters.

The exit Command.

The [[...]] Command.

Checking Strings.

Checking Patterns.

Checking File Attributes.

Checking Integer Attributes.

The ! Operator.

Compound Expressions.

&& - The AND Operator. | | - The OR Operator.

[[...]] vs test and [...].

Checking File Descriptors.

Control Commands.

The case Command.

Specifying Patterns with case.

The for Command.

Other for Syntax.

The if Command.

Other if Syntax: else.

Other if Syntax: elif. if/elif vs case.

The while Command.

The until Command.

Nested Loops.

Breaking Out of Loops.

The continue Command.

The select Command.

Other select Syntax.

The select Menu Format.


Input/Output Commands.

The print Command.

Escape Characters.

print Options.

The echo Command.

The exec Command.

The read Command.

Reading Input from Files.

The IFS Variable.

More with read.

Reading Input Interactively.

The REPLY variable.

Miscellaneous Programming Features.

The Command.


Returning Function Exit Status.

Scope and Availability.

Using Functions in Korn Shell Scripts.

Function Variables.

Displaying Current Functions.

Autoloading Functions.

Discipline Functions.


Removing Function Definitions.


Ignoring Signals.

Resetting Traps.

Exit and Function Traps.

Debugging with Trap.

Trap Signal Precedence.

Trapping Keyboard Signals.

Debugging Korn Shell Scripts.

Enabling Debug Options. Debugging Example.

Debugging with Trap.

Parsing Command-line Arguments.

More with Here Documents.


9. Miscellaneous Commands.

The : Command.

The eval Command.

The export Command.

The False Command.

The newgrp Command.

The pwd Command.

The Readonly Command.

The Set Command.

The Time Command.

The Times Command.

The True Command.

The ulimit Command.

The umask Command.

The whence Command.

Appendix A: Sample .profile File.

Appendix B: Sample Environment File.

Appendix C: C Shell Functionality. Directory Functions. Miscellaneous Commands. The .logout File. The chdir Command. The logout Command. The setenv Command. The source Command.

Appendix D: Sample Korn Shell Scripts. Display Files - kcat. Interactive uucp - kuucp. Basename - kbasename. Dirname - kdirname. Display Files with Line Numbers - knl. Find Words - match. Simple Calculator - kcalc. Search for Patterns in Files - kgrep. Calendar Program - kcal.

Appendix E: Korn Shell Man Page.

Appendix F: Pdksh.

Appendix G: Pdksh Quick Reference.

Appendix H: Pdksh Man Page.

Index. 0201675234T05022001


Korn Shell 93: The Latest Version

The Korn Shell User and Programming Manual is designed to be a reference and learning tool for a range of users - from the novice with some experience to the pro who is familiar with both the Bourne and C shells. It contains complete technical information, as well as hands-on examples and complete programs to help guide you and illustrate all the features of the Korn shell. This edition of the book has been updated to cover Korn Shell 93, the latest version of the Korn shell. This book also assumes that you are familiar with the basic Unix commands, and understand file system concepts. You should also be able to login to a system, and enter basic commands.

If you are an experienced user, you may want to skip Chapter 1 and the first half of Chapter 2. The first seven chapters deal primarily with interactive use, while Chapter 8 and 9 cover the programming concepts.

The goal of this book to teach you the Korn shell, and this is done by walking you through examples. So by the time you are finished reading the book, you'll be comfortable with it, and writing your own Korn shell scripts.

But don't just read the book. The best way for you to learn about the Korn shell is to type in the examples yourself. Then do some experimentation on your own by either modifying the examples or coming up with your own commands.

Chapter 1 contains an overview of the major features in the Korn shell. It covers where to get it, how your login shell is configured, and setting up the Korn shell to co-exist with other shells while you are on the learning curve. It also includes brief descriptions of other related shells, including the Born Again shell (bash), Mortice Kern shell (ksh) for PC/Windows, and the public domain Korn shell (pdksh) for Linux.

Chapter 2 covers the Korn shell basics: how commands can work together to do other things, and some basic shortcuts to improve productivity, and Korn shell I/O. You'll also be introduced to file name, command, and tilde substitution: important concepts that are the basis of much of the Korn shell.

Chapter 3 teaches you about Korn shell variables, variable attributes, and parameters. You'll learn about all the different types of variable expansion, including the substring features. Array variables and quoting are also discussed in detail.

Chapter 4 discusses the Korn shell command history mechanism and vi and emacs in-line editors. Here you will learn how to call up previous commands and manipulate them.

Chapter 5 shows you how to manage and manipulate multiple processes using the job control mechanism, a feature almost directly copied from the C shell.

In Chapter 6, you will learn how to perform arithmetic with the Korn shell. It contains sections on multi-base arithmetic, declaring integer- type variables, and random numbers, along with examples for each type of arithmetic operator.

Chapter 7 will show you how to set up your own customized environment: from setting up the prompt how you like it, to configuring your personal email. Korn shell options, environment variables, aliases, the .profile file, and subshells are also covered.

In Chapter 8, you are taught how to write programs using the many Korn shell commands and features. Executing and debugging scripts, input/output commands, positional parameters, flow control commands such as case, for, if, select, while, and until are also discussed. Step-by-step examples are included, and complete usable scripts are built from the bottom up. For those experienced Unix programmers, important differences between the Korn and Bourne shells are discussed, and something else new to Unix shell programming - performance. You'll learn a few tricks that can speed up execution of your Korn shell scripts.

Chapter 9 covers miscellaneous commands, such as readonly, ulimit, whence, and Korn shell functions.

Appendix A and B include a sample ready-to-use profile and environment file.

Appendix C contains the Korn shell versions of a number of C shell commands and functions.

Appendix D contains the source code listing for a number of handy ready-to-run Korn shell scripts, including an interactive calendar program.

Appendix E contains the Korn shell man pages.

Appendix F contains information about pdksh, the public domain version of the Korn shell for Linux.

Appendix G contains the Pdksh quick reference guide, and Appendix H contains the Pdksh man page.

This edition is based on the latest edition of the Korn shell. There have been a number of new features and enhancements added to Korn Shell 93 including:

  • Datatypes: New data types: floats and structures.
  • Variables: New variable typer: compound and nameref variables.
  • Arrays: Associative arrays and additional commands for array manipulation.
  • Functions: Discipline functions to support further manipulation of variables.
  • String Manipulation: Search, replace, and substring operators.


Thanks to David Korn for developing the Korn shell, Steven Bourne for the Bourne shell, and Bill Joy for the C shell. Other thanks to Mike Veach and Pat Sullivan for contributing to the development of the Korn shell, and Mark Horton, Bill Joy (again), and Richard Stallman for developing the vi and emacs editors which were used in the development of the Korn shell.

Special thanks to Peter Collinson, Cynthia Duquette, Ian Jones, Peter Kettle, Heather Prenatt, the ASP staff, Aspen Technologies, O'Reilly & Associates (who reviewed the initial draft of this book before publishing their own Korn shell book!), James Lamm, Darian Edwards and others for reviewing drafts of this book.


The information and material has been tested and verified to the best of our ability. This does not mean that it is always 100% correct! Subtle differences and variations may exist between implementations and features may have changed. And of course there may even be bugs or mistakes! Please send us any comments or suggestions for future editions, along with information about your environment. Please visit our Web site for more information www.aw.com/cseng.

Source Code Listing

If you would like the source code listing to the Korn shell scripts listed in the appendices, please visit our web site at www.aw.com/cseng.

Licensing Agreement

This book comes with a CD software package. By opening this package you are agreeing to be bound by the license terms contained on the CD- ROM.



Submit Errata

More Information

Unlimited one-month access with your purchase
Free Safari Membership