# info > ksh

---
type: CommandReference
command: ksh
mode: man
section: 1
source: man-pages
---

## Quick Reference

- `ksh` — start interactive KornShell session
- `ksh -c 'command'` — execute command string
- `ksh script.sh` — execute script file
- `varname=value` — assign variable
- `for i in 1 2 3; do echo $i; done` — for loop over words
- `if [[ -f file ]]; then echo exists; fi` — conditional test
- `case $var in pattern) cmd;; esac` — pattern matching
- `$(command)` — command substitution
- `(( expr ))` — arithmetic evaluation, exit 0 if non-zero

## Name

**ksh**, **rksh** — KornShell, a standard/restricted command and programming language

## Synopsis

shell
ksh [ ±abcefhiklmnprstuvxBCDEGH ] [ ±o option ] ... [ - ] [ arg ... ]
rksh [ ±abcefhiklmnpstuvxBCDEGH ] [ ±o option ] ... [ - ] [ arg ... ]
## Options

Shell invocation and `set` built-in options. Options can be turned on with `-` and off with `+`.

**File and execution control**:
- `-a` — export all subsequently defined variables
- `-e` — exit on error (non-zero exit status)
- `-f` — disable pathname expansion (glob)
- `-h` — track each command (create tracked alias)
- `-k` — (obsolete) place all variable assignments in environment
- `-n` — read commands but do not execute (syntax check)
- `-t` — (obsolete) exit after reading one command
- `-u` — treat unset variables as error on substitution
- `-v` — print shell input lines as read
- `-x` — print commands and arguments as executed

**Job control and interactive**:
- `-b` — print job completion messages immediately
- `-m` — enable job control (monitor mode)
- `-p` — privileged mode (disable .profile, use /etc/suid_profile)

**Editing modes**:
- `-o emacs` — Emacs-style line editing
- `-o gmacs` — GNU Emacs-style line editing
- `-o vi` — vi-style line editing
- `-o viraw` — raw mode for vi editing

**Other set options**:
- `-B` — enable brace expansion (default)
- `-C` — prevent `>` from truncating existing files (noclobber)
- `-G` — enable recursive glob (`**`)
- `-H` — enable `!`-style history expansion
- `-o allexport` — same as `-a`
- `-o errexit` — same as `-e`
- `-o globstar` — same as `-G`
- `-o histexpand` — same as `-H`
- `-o ignoreeof` — do not exit on EOF
- `-o keyword` — same as `-k`
- `-o letoctal` — let recognises octal numbers starting with 0
- `-o monitor` — same as `-m`
- `-o noclobber` — same as `-C`
- `-o noglob` — same as `-f`
- `-o notify` — same as `-b`
- `-o nounset` — same as `-u`
- `-o pipefail` — pipeline returns exit status of last non-zero command
- `-o posix` — enable POSIX compliance mode
- `-o privileged` — same as `-p`
- `-o trackall` — same as `-h`
- `-o verbose` — same as `-v`
- `-o xtrace` — same as `-x`

**Special invocation options**:
- `-c` — execute commands from the first argument (script string)
- `-s` — read commands from stdin
- `-i` — interactive shell
- `-r` — restricted shell (rksh)
- `-D` — print all double-quoted strings preceded by `$` for i18n
- `-l` — login shell
- `-E` or `-o rc` — read ENV file (default for interactive)

## Built-in Commands

Special built-ins (marked with †) affect variable assignments and script execution. Declaration commands (marked with ‡) allow variable assignments with tilde expansion and array syntax.

**Special built-ins**:
- `† : [arg...]` — expand parameters, do nothing
- `† . name [arg...]` — read file or execute function in current environment
- `† break [n]` — exit from loop
- `† continue [n]` — resume next iteration of loop
- `† eval [arg...]` — read and execute arguments as shell input
- `† exec [-c] [-a name] [arg...]` — replace shell with command; or modify redirections
- `† exit [n]` — exit shell with status n
- `† export [-p] [name[=value]...]` — mark variables for export
- `† readonly [-p] [vname[=value]...]` — mark variables read-only
- `† return [n]` — return from function or dot script
- `† set [options] [arg...]` — set options and positional parameters
- `† shift [n]` — shift positional parameters
- `† times` — display accumulated CPU times
- `† trap [-p] [action] [sig...]` — set signal traps
- `† typeset [options] [vname[=value]...]` — set variable attributes and values
- `† unset [-fnv] vname...` — unset variables or functions

**Other important built-ins**:
- `alias [-ptx] [name[=value]...]` — define or list aliases
- `autoload name...` — mark functions for autoloading from FPATH
- `bg [job...]` — put jobs in background
- `cd [-LeP] [arg]` or `cd old new` — change directory
- `command [-pvxV] name [arg...]` — run command bypassing functions/aliases
- `echo [arg...]` — print arguments (system-dependent for escapes)
- `eval [arg...]` — execute arguments as shell commands
- `exec [-c] [-a name] [arg...]` — replace shell or modify redirections
- `exit [n]` — exit shell
- `export [-p] [name[=value]...]` — export variables
- `false` — do nothing, exit 1
- `fc [-e ename] [-nlr] [first [last]]` — edit and re-execute history commands
- `fg [job...]` — bring jobs to foreground
- `getconf [name [pathname]]` — print system configuration values
- `getopts [-a name] optstring vname [arg...]` — parse command options
- `hash [-r] [utility]` — display or modify tracked alias hash table
- `hist [-e ename] [-nlr] [first [last]]` — history command editor
- `hist -s [old=new] [command]` — re-execute history command with substitution
- `jobs [-lnp] [job...]` — list active jobs
- `kill [-s signame] job...` or `kill -n signum job...` — send signal
- `let arg...` — evaluate arithmetic expressions
- `print [-CRenprsv] [-u unit] [-f format] [arg...]` — formatted output
- `printf [-v vname] format [arg...]` — formatted output with C-style format
- `pwd [-LP]` — print working directory
- `read [-ACSprsv] [-d delim] [-n n] [-N n] [-t timeout] [-u unit] [vname...]` — read input
- `readonly [-p] [vname[=value]...]` — mark variables read-only
- `redirect` — modify file descriptors persistently
- `source name [arg...]` — same as `.` (non-special)
- `test expression` — evaluate conditional expressions (same as `[`)
- `true` — do nothing, exit 0
- `type name...` — show how each name would be interpreted (same as `whence -v`)
- `ulimit [-HSa...] [limit]` — get or set resource limits
- `umask [-S] [mask]` — set or print file creation mask
- `unalias [-a] name...` — remove aliases
- `wait [job...]` — wait for background jobs
- `whence [-afpqv] name...` — show how name would be interpreted

## Examples

**Process substitution** (from man page):
shell
paste <(cut -f1 file1) <(cut -f3 file2) | tee >(process1) >(process2)
**Arithmetic evaluation**:
shell
(( x = 3 * 4 + 1 ))
echo $(( 2**10 ))
**Parameter expansion with default**:
shell
echo ${d:-$(pwd)}   # prints pwd output only if d is unset or null
**Pattern matching for substring**:
shell
name="hello_world"
echo ${name#*_}    # prints "world"
echo ${name%_*}    # prints "hello"
**Here document with indentation** (using `<<-`):
shell
cat <<-EOF
    indented text
EOF
**Named background job pool**:
shell
& poolname sleep 10 &
**Using `select` for menu**:
shell
select fruit in apple banana cherry; do
  echo "You chose $fruit"
  break
done
## See Also

- [cat(1)](http://localhost/phpMan.php/man/cat/1/markdown), [cd(1)](http://localhost/phpMan.php/man/cd/1/markdown), [chmod(1)](http://localhost/phpMan.php/man/chmod/1/markdown), [echo(1)](http://localhost/phpMan.php/man/echo/1/markdown), [env(1)](http://localhost/phpMan.php/man/env/1/markdown), [exec(2)](http://localhost/phpMan.php/man/exec/2/markdown), [fork(2)](http://localhost/phpMan.php/man/fork/2/markdown), [pipe(2)](http://localhost/phpMan.php/man/pipe/2/markdown), [printf(3)](http://localhost/phpMan.php/man/printf/3/markdown), [stty(1)](http://localhost/phpMan.php/man/stty/1/markdown), [test(1)](http://localhost/phpMan.php/man/test/1/markdown), [ulimit(2)](http://localhost/phpMan.php/man/ulimit/2/markdown), [wait(2)](http://localhost/phpMan.php/man/wait/2/markdown), [environ(7)](http://localhost/phpMan.php/man/environ/7/markdown)
- Morris I. Bolsky and David G. Korn, *The New KornShell Command and Programming Language*, Prentice Hall, 1995.
- POSIX.2: Shell and Utilities, IEEE Std 1003.2-1992.

## Exit Codes

Errors detected by the shell (e.g., syntax errors) cause a non-zero exit status. If non-interactive, the shell abandons execution of the script unless the error occurs inside a subshell. Otherwise, the shell returns the exit status of the last command executed. Run-time errors print the command/function name and error condition; line numbers >1 are shown in square brackets.