Llywelyn.net


Contents

On this page:

  1. Invocation Options
  2. Built-in Commands and Keywords
  3. Built-in Shell Variables
  4. set -o Options
  5. Basic Korn Shell Operators
  6. Substitutions
  7. Regular Expression Operators
  8. Double Operators
  9. String Comparison Operators
  10. Test Operators
  11. Arithmetic and Relational Operators
  12. Typeset Options
    External links:
  13. Korn Shell Programming Tutorial
  14. Kornshell.com
  15. More Comprehensive Lists and Examples

Invocation Options
Back to top Table of Contents.

These options can be used when invoking the Korn shell. In addition, any set option can be used on the command-line. Login shells are usually invoked with the options -i (interactive), -s (read from standard input), and -m (enable job control).
OptionMeaning
-c stringExecute string, then exit.
-sRead commands from the standard input. If an argument is given, this flag takes precedence (i.e., the argument won't be treated as a script name and standard input will be read).
-iInteractive shell. Ignore signals TERM, INTR, and QUIT.
-rRestricted shell.

Built-in Commands and Keywords
Back to top Table of Contents.

CommandMeaningCommandMeaning
:Do nothing (just do expansions of arguments).letArithmetic variable assignment.
.Read file and execute its contents in current shell.newgrpStart new shell with new group ID.
aliasSet up shorthand for command or command-lineprintExpand and print arguments on standard output.
bgPut job in background.pwdPrint working directory.
breakExit from surrounding for, select, while, or until loop.readRead a line from standard input.
caseMulti-way conditional construct.readonlyMake variables read-only (unassignable).
cdChange working directory.returnReturn from surrounding function or script.
continueSkip to next iteration of for, select, while, or until loop.selectMenu generation construct.
echoExpand and print arguments.setSet options.
execReplace shell with given program.shiftShift command-line arguments.
exitExit from shell.timeRun command and print execution times.
exportCreate environment variables.trapSet up signal-catching routine.
evalProcess arguments as a command line.typesetSet special characteristics of variables.
fcFix command (edit history file).ulimitSet/show process resource limits.
fgPut background job in foreground.umaskSet/show file permission mask.
forLooping construct.unaliasRemove alias definitions.
functionDefine function.unsetRemove definitions of variables or functions.
getoptsProcess command-line options.untilLooping construct.
ifConditional construct.waitWait for background job(s) to finish.
jobsList background jobs.whenceIdentify source of command.
killSend signal to process.whileLooping construct.

Built-in Shell Variables
Back to top Table of Contents.

VariableMeaningDefaultSet by:
#Number of arguments given to current process.  
-Options given to shell on invocation.See above. 
?Exit status of previous command.noneksh
$Process ID of shell process.noneksh
_Last argument to previous command.noneksh
!Process ID of last background command.noneksh
CDPATHList of directories for cd command to search.noneUser
COLUMNSWidth of display in columns (for editing modes and select).80System Administrator
EDITORUsed to set editing mode; also used by mail and other programs./bin/edUser, System Administrator
ERRNOError number of last system call that failed.noneksh
ENVName of file to run as environment file when shell is invoked.noneUser, System Administrator
FCEDITDefault editor for fc command./bin/edUser, System Administrator
FPATHSearch path for autoloaded functions.noneUser
HISTFILEName of command history file.$HOME/.sh_historyUser, System Administrator
HISTSIZENumber of lines kept in history file.128User, System Administrator
HOMEHome (login) directory.noneSystem Administrator
IFSInternal field separator; list of characters that act as word separators.SPACE, TAB, and NEWLINE.User
LINENONumber of line in script or function that just ran.noneksh
LINESHeight of display in line (for select command).24System Administrator
LOGNAMEUser name.noneSystem Administrator
MAILName of file to check for new mail.noneSystem Administrator
MAILCHECKHow often (in seconds) to check for new mail.600 secondsUser, System Administrator
MAILPATHList of file names to check for new mail, if MAIL is not set.noneSystem Administrator
OLDPWDPrevious working directory.noneksh
OPTARGOption to argument being processed by getopts.noneksh
OPTINDNumber of first argument after options.noneksh
PATHSearch path for commands./bin:/usr/binUser, System Administrator
PPIDProcess ID of parent process.noneksh
PS1Primary command prompt string.$User
PS2Prompt string for line continuations.>User
PS3Prompt string for select command.#?User
PS4Prompt string for xtrace option.+User
PWDCurrent working directory.noneUser
RANDOMRandom number between 0 and 32767 (215 - 1)noneksh
REPLYUser's response to select command; result of read command if no variable names given.noneSystem Administrator
SECONDSNumber of seconds since shell was invoked.noneksh
SHELLFull pathname of shell.noneSystem Administrator
TERMType of terminal user is utilizing.noneSystem Administrator
TMOUTIf set to positive integer, number of seconds between commands after which shell automatically terminates.0 (unlimited)ksh
VISUALUsed to set editing mode./bin/edUser, System Administrator

set -o Options
Back to top Table of Contents.

these are options that can be turned on with the set -o command. All are initially off except where noted. Abbrevs, where listed, are arguments to set that can be used instead of the full set -o command (e.g., set -a is an abbreviation for set -o allexport). the abbreviations are actually backward-compatible Bourne shell options.
OptionAbbreviationMeaning
allexport-aExport all subsequently defined variables.
errexit-eExit the shell when a command exits with a non-0 status.
bgnice Run all background jobs at decreased priority (on by default).
emacs Use emacs-style command-line editing.
gmacs Use emacs-style command-line editing, but with a slightly different meaning for CTRL-T.
ignoreeof Disallow CTRL-D to exit the shell.
markdirs Add / to all directory names generated from wildcard expansion.
monitor-mEnable job control (on by default).
noclobber Don't allow > redirection to existing files.
noexec-nRead commands and check for syntax errors, but don't execute them.
noglob-fDisable wildcard expansion.
nolog Disable command history.
nounset-uTreat undefined variables as errors, not as null.
privileged-pScript is running in suid mode.
trackall-hSubstitute full pathnames for commands in alias expansions.
verbose-vPrint commands (verbatim) before running them.
vi Use vi-style command-line editing.
viraw Use vi mode and have each keystroke take effect immediately.
xtrace-xPrint commands (after expansions) before running them.

Basic Korn Shell Operators
Back to top Table of Contents.

~ Home Directory{ Start Code Block
# Comment} End Code Block
$ Variable Expressions; Shell Command Separator
& Background Job' Strong Quote
* String Wildcard" Weak Quote
( Start Subshell< Input Redirect
) End Subshell> Output Redirect
\ Quote Next Character/ Pathname Directory Separator
| Pipe? Single-character Wildcard
[ Start Character-set Wildcard] End Character-set Wildcard

Substitutions
Back to top Table of Contents.

${varname:-word} Return a default value if varname is undefined
${varname:=word} Set a variable to a default value if varname is undefined
${varname:?message} If varname is undefined, print message and terminate script.
${varname:+word} If varname exists, return word, otherwise return nothing.

Regular Expression Operators
Back to top Table of Contents.

OperatorsExamples
OperatorMeaningExpressionMatches
 xx
*(exp)0 or more occurrences of exp*(x)Null string, x, xx, xxx,...
+(exp)1 or more occurrences of exp+(x)x, xx, xxx, ...
?(exp)0 or 1 occurrences of exp?(x)Null string, x
@(exp1|exp2|...)exp1 or exp2 or ...@(x)x
!(exp)Anything that does not match exp!(x)Any string except x

Double Operators
Back to top Table of Contents.

SyntaxMeaning
statement1 && statement2Execute statement1, and if its exit status is 0, execute statement2
statement1 || statement2Execute statement1, and if its exit status is not 0, execute statement1.

String Comparison Operators
Back to top Table of Contents.

OperatorTrue If...OperatorTrue If...
str=pat*str matches patstr1>str2str1 is greater than str2
str!=patstr does not match pat-n strstr is not null (has length greater than 0)
str1<str2str1 is less than str2-z strstr is null (has length 0)

Test Operators
Back to top Table of Contents.

These are the operators used with the [[...]] construct.
They can be logically combined with && ("and") and || ("or") and grouped with parentheses.
OperatorTrue If...OperatorTrue If...
-a filefile exists-x fileYou have execute permission on file, or directory search permission if it is a directory.
-d filefile is a directory-G fileYour group ID is the same as that of file.
-f filefile is a regular file (i.e. not a directory or other special type of file)-L filefile is a symbolic link.
-r fileYou have read permission on file-O fileYou own file.
-s filefile exists and is not emptyfile1 -nt file2file1 is newer than file2
-w fileYou have write permission on filefile1 -ot file2file1 is older than file2.

Arithmetic and Relational Operators
Back to top Table of Contents.

Arithmetic OperatorsRelational Operators
OperatorMeaningOperatorMeaning
+Plus 
-Minus<Less than
*Times>Greater than
/Division (with truncation)<=Less than or equal to
%Remainder>=Greater than or equal to
<<Bit-shift left==Equal
>>Bit-shift right!=Not equal
&Bitwise and&&Logical and
|Bitwise or||Logical or
~Bitwise not 
^Bitwise exclusive or 

Typeset Options
Back to top Table of Contents.

These are the arguments to the typeset command.
 With no option, create local variable within function.
-LLeft justify and remove leading blanks.
-RRight justify and remove trailing blanks.
-fWith no arguments, prints all function definitions.
-f fnamePrints the definition of function fname.
+fPrints all function names.
-ftTurns on trace mode for named function(s).
+ftTurns off trace mode for named function(s).
-fuDefines given name(s) as autoloaded function(s).
-iDeclare variable as integer.
-lConvert all letters to lowercase.
-rMake variable read-only.
-uConvert all letters to uppercase.
-xExport variable, i.e., put in environment so it is passed to subshells.
Back to top Table of Contents.