LinuxCommandLibrary

ksh

Execute commands using the Korn shell

TLDR

Start an interactive shell session

$ ksh
copy

Execute specific [c]ommands
$ ksh -c "[echo 'ksh is executed']"
copy

Execute a specific script
$ ksh [path/to/script.ksh]
copy

Check a specific script for syntax errors without executing it
$ ksh -n [path/to/script.ksh]
copy

Execute a specific script, printing each command in the script before executing it
$ ksh -x [path/to/script.ksh]
copy

SYNOPSIS

ksh [+/-abcefhimnprstuvx] [+/-o option] [command_file [argument...]]
ksh -c command_string [command_name [argument...]]
ksh -s [argument...]

PARAMETERS

-a
    Automatically export all subsequent defined parameters.

-b
    Perform background jobs asynchronously (job control is enabled by default in interactive shells).

-c command_string
    Read commands from the command_string argument. Any remaining arguments are assigned to the positional parameters, starting with $0.

-e
    Exit immediately if a command exits with a non-zero status. This is equivalent to `set -o errexit`.

-i
    Force the shell to be interactive. An interactive shell ignores `SIGTERM` and `SIGINT`, and enables job control.

-n
    Read commands but do not execute them. Useful for checking script syntax without running it.

-p
    Enter privileged mode. The `ENV` file is not processed, and functions are not imported from the environment. This helps prevent security vulnerabilities.

-s
    Read commands from standard input. Any remaining arguments are assigned to the positional parameters.

-u
    Treat unset parameters as an error when performing parameter expansion. Equivalent to `set -o nounset`.

-v
    Print shell input lines as they are read. Equivalent to `set -o verbose`.

-x
    Print commands and their arguments as they are executed. Equivalent to `set -o xtrace`.

-o option
    Enable or disable specific shell options. Common options include:
emacs (emacs-style command-line editing),
vi (vi-style command-line editing),
monitor (enable job control),
noclobber (prevent redirection from overwriting existing files).

DESCRIPTION

The KornShell (ksh) is a Unix shell developed by David Korn at Bell Labs in the early 1980s. It was designed to be a superset of the original Bourne Shell (sh) while incorporating many features from the C Shell (csh), along with its own significant enhancements.

ksh offers robust command-line editing capabilities (with vi and emacs modes), command history, built-in arithmetic, arrays, and advanced I/O features like co-processes. It's renowned for its powerful scripting language, which includes functions, aliases, and sophisticated flow control mechanisms, making it ideal for system administration tasks, automation, and complex script development.

While providing a rich programming environment, ksh also excels as an interactive command interpreter, balancing ease of use with comprehensive functionality. Its influence can be seen in other modern shells like Bash and Zsh.

CAVEATS

Different versions of ksh (notably ksh88 and ksh93) have varying features and behaviors; scripts relying on ksh93 specific features may not run on older ksh88 implementations. While generally stable, older ksh versions had security vulnerabilities like Shellshock, underscoring the importance of keeping your shell updated. Features like co-processes might behave slightly differently across various Unix-like systems.

INTERACTIVE FEATURES

ksh provides advanced interactive features including a powerful command history with configurable length, and two primary modes for command-line editing: vi mode (for users familiar with the vi editor) and emacs mode (for users familiar with the emacs editor). These modes allow efficient navigation, editing, and searching of past commands.

PROGRAMMING CAPABILITIES

Beyond interactive use, ksh is a full-fledged programming language. It supports functions with local variables, aliases, arrays, and co-processes (which allow two-way communication with other programs). It also includes sophisticated control structures like `select` loops, and robust error handling via the `trap` command, making it suitable for developing complex and reliable scripts.

VERSION DIFFERENCES

The two main versions are ksh88 and ksh93. ksh93 is a significant update, introducing features such as floating-point arithmetic, associative arrays, `select` loops, dynamic aliases, and enhanced I/O redirection. When writing scripts, it's important to be aware of which ksh version is expected to be available, as some advanced features of ksh93 are not present in ksh88.

HISTORY

KornShell (ksh) was developed by David Korn at Bell Labs in the early 1980s, with the goal of creating a more robust and feature-rich shell than the existing Bourne Shell (sh) and C Shell (csh). Its first widely adopted version was ksh88.

A significant update, ksh93, introduced major enhancements including floating-point arithmetic, associative arrays, new built-in commands, and improved scripting capabilities, making it a powerful programming language in its own right. ksh's innovations in command-line editing, job control, and scripting constructs profoundly influenced the development of other popular shells like Bash and Zsh. It continues to be maintained and used, particularly in enterprise Unix environments, for its stability and powerful features.

SEE ALSO

sh(1), bash(1), zsh(1), csh(1), dash(1), shells(1)

Copied to clipboard