Go to the first, previous, next, last section, table of contents.

# Korn Shell Style Features

This section describes features primarily inspired by the Korn Shell (`ksh`). In some cases, the Posix 1003.2 standard has adopted these commands and variables from the Korn Shell; Bash implements those features using the Posix standard as a guide.

- Korn Shell Constructs: Shell grammar constructs adopted from the Korn Shell
- Korn Shell Builtins: Builtin commands adopted from the Korn Shell.
- Korn Shell Variables: Variables which bash uses in essentially the same way as the Korn Shell.
- Aliases: Substituting one command for another.

## Korn Shell Constructs

Bash includes the Korn Shell `select` construct. This construct allows the easy generation of menus. It has almost the same syntax as the `for` command.

The syntax of the `select` command is:

    select name [in words ...]; do commands; done

The list of words following `in` is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the "`in words`" is omitted, the positional parameters are printed. The `PS3` prompt is then displayed and a line is read from the standard input. If the line consists of the number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If `EOF` is read, the `select` command completes. Any other value read causes name to be set to null. The line read is saved in the variable `REPLY`.

The commands are executed after each selection until a `break` or `return` command is executed, at which point the `select` command completes.

## Korn Shell Builtins

This section describes Bash builtin commands taken from `ksh`.

`fc`

      fc [-e ename] [-nlr] [first] [last]
      fc -s [pat=rep] [command]

  Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If the `-l` flag is given, the commands are listed on standard output. The `-n` flag suppresses the command numbers when listing. The `-r` flag reverses the order of the listing. Otherwise, the editor given by ename is invoked on a file containing those commands. If ename is not given, the value of the following variable expansion is used: `${FCEDIT:-${EDITOR:-vi}}`. This says to use the value of the `FCEDIT` variable if set, or the value of the `EDITOR` variable if that is set, or `vi` if neither is set. When editing is complete, the edited commands are echoed and executed. In the second form, command is re-executed after each instance of pat in the selected command is replaced by rep. A useful alias to use with the `fc` command is `r='fc -s'`, so that typing `r cc` runs the last command beginning with `cc` and typing `r` re-executes the last command (see section Aliases).
`let`
  The `let` builtin allows arithmetic to be performed on shell variables. For details, refer to section Arithmetic Builtins.
`typeset`
  The `typeset` command is supplied for compatibility with the Korn shell; however, it has been made obsolete by the `declare` command (see section Bash Builtin Commands).

## Korn Shell Variables

`REPLY`
  The default variable for the `read` builtin.
`RANDOM`
  Each time this parameter is referenced, a random integer is generated. Assigning a value to this variable seeds the random number generator.
`SECONDS`
  This variable expands to the number of seconds since the shell was started. Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.
`PS3`
  The value of this variable is used as the prompt for the `select` command.
`PS4`
  This is the prompt printed before the command line is echoed when the `-x` option is set (see section The Set Builtin).
`PWD`
  The current working directory as set by the `cd` builtin.
`OLDPWD`
  The previous working directory as set by the `cd` builtin.
`TMOUT`
  If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after that number of seconds if input does not arrive.

## Aliases

- Alias Builtins: Builtins commands to maniuplate aliases.

The shell maintains a list of aliases that may be set and unset with the `alias` and `unalias` builtin commands.

The first word of each command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `=`. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias `ls` to `"ls -F"`, for instance, and Bash does not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

Aliases are created and listed with the `alias` command, and removed with the `unalias` command.

There is no mechanism for using arguments in the replacement text, as in `csh`. If arguments are needed, a shell function should be used.

Aliases are not expanded when the shell is not interactive.

The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. This means that the commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when the function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use `alias` in compound commands.

Note that for almost every purpose, aliases are superseded by shell functions.

### Alias Builtins

`alias`

      alias [name[=value] ...]

  Without arguments, print the list of aliases on the standard output. If arguments are supplied, an alias is defined for each name whose value is given. If no value is given, the name and value of the alias is printed.
`unalias`

      unalias [-a] [name ... ]

  Remove each name from the list of aliases. If `-a` is supplied, all aliases are removed.

Go to the first, previous, next, last section, table of contents.