Korn Shell Notes

1 Useful Cmds

wc - display a count of lines, words and characters in a file.
    It can be used to count the numbers together with other commands, such as ls, who and so on.

cut - cut out selected fields of each line of a file.
    The name of each unique user that is logged on is displayed using this command:
        $ who | cut —f1 —d' ' | sort —u



print is a Korn shell specialized command which is similar to echo.

nl - line numbering filter.

2 Process Execution

2.1 Conditional Execution

You can implement a simple if command by using the && and || operators together like this:
            command1 && command2 || command3
If command1 returns true, then command2 is executed, which causes
command3 to not be executed. If command1 does not return true, then
command2 is not executed, which causes command3 to be executed.

2.2 Grouping Commands

Commands enclosed in {} are executed in current shell. If

        $ echo "This is file temp:" ; cat temp | nl
        This is file temp:
        1 The rain in Spain
        2 falls mainly on the plain
then by using {}s, the output will be:
        $ { echo "This is file temp:"; cat temp ; } | nl
        1 This is file temp:
        2 The rain in Spain
        3 falls mainly on the plain
There must be whitespace after the opening {, and commands within the {}'s must be terminated with a semi-colon.

enclosed in () are executed in a subshell. Subshells are generated
whenever you enclose commands in ()'s, perform command substitution,
for background processes, and for coprocesses.A subshell is a separate
copy of the parent shell, so variables, functions, and aliases from the
parent shell are available to the subshell. However, subshells cannot
change the value of parent shell variables, functions, or aliases.

3 Input/Output Redirection

3.1 Redirecting Standard Output

By using the > symbol, the standard output of a command can be redirected to a file, instead of your terminal.

You can also use the > command to create an empty file:
    $ > tmp
    $ ls —s tmp
    0 tmp
This is equivalent to touch tmp.

Standard output can be appended to a file by using the >> redirect operator.

Standard output is closed with the >&- redirect operator:
    $ echo "This is going nowhere" >&-

3.2 The noclobber Option

prevent redirecting output to an existing file, set the noclobber
option. By default, this feature is usually disabled, but can be
enabled with the set -o noclobber command:
        $ ls > file.out
        $ set —o noclobber

The >| operator is used to force overwriting of a file, even if the noclobber option is enabled.

3.3 Redirecting Standard Input

Standard input to a command is redirected from a file using the < operator.

3.4 Redirecting Standard Error

with standard output, standard error is by default directed to your
terminal, but it can be redirected using the standard error file
descriptor (2) and the > operator, e.g. 2>

3.5 More With File Descriptors

The >&n operator
causes output to be redirected to file descriptor n. This is used when
you want to direct output to a specific file descriptor.In the next
command, the standard error and output are sent to ls.out by specifying
multiple redirections on the same command line. First, >&2
causes standard output to be redirected to standard error. Then,
2>ls.out causes standard error (which is now standard output and
standard error) to be sent to ls.out:
        $ ls tmp t.out 2>ls.out 1>&2
        $ cat ls.out
        tmp not found

the output of the last command was redirected to a file using the >
operator, then only the standard output would be redirected. The
standard error would still be displayed on your terminal.
        $ {echo "This is going to stdout"} >&1;/
        echo "This is going to stderr" >&2;} >out
        This is going to stderr
        $ cat out
        This is going to stdout.

The n>&m operator
causes the output from file descriptors n and m to be merged. This
operator could be used in the previous command to direct both the
standard output and standard error to the same file.

If you wanted the standard output and standard error appended to the output file, the >> operator could be used.

3.6 Here Document

Here documents is a term used for redirecting multiple lines of
standard input to a command. In looser terms, they allow batch input to
be given to programs and are frequently used to execute interactive
commands from within Korn shell scripts. The format for a here document
        command << word
        command <<-word
where lines that follow are used as standard input to command until word is read.

other variation, command <<- word, deletes leading tab characters
from the here document. It is often used over the first variation to
produce more readable code when used within larger scripts. A good use
for here documents is to automate ftp file transfers. This snippet of
code could be used within a larger script to automatically ftp code to
or from another server.

3.7 Discarding Standard Output

  The /dev/null file is like the system trash bin. Anything redirected to it is discarded by the system.

4 File Name Substitution

4.1 The * Character

* matches any zero or more characters in current directory, except
those begin with the "." character. These must be explicitly matched.

4.2 The ? Character

The ? character matches exactly one character.

4.3 The [] Character

The [] characters match a single, multiple, or range of characters.
This command lists file names beginning with d or m, and ending with any number 1 through 5:

the range argument, the first character must be alphabetically less
than the last character. This means that [c-a] is an invalid range.
This also means that pattern [0–-z] matches any alphabetic or
alphanumeric character, [A-z] matches any alphabetic character (upper
and lower case), [0–Z] matches any alphanumeric or upper case
alphabetic character, and [0–9] matches any alphanumeric character.

Multiple ranges can also be given. The pattern [a-jlmr3–-7] would match files names beginning with the letters a through j, l, m, r, and 3 through 7.

4.4 The ! Character

The ! character can be used with [ ] to reverse the match. In other words, [!a] matches any character, except a.
If we want to list all of the file names except those started with d, we could do this:

or it can be done more easily using [!d]:

Multiple and range arguments can also be negated. The pattern [!lro]* would match strings not beginning with l, r, or o, *[!2–5] would match strings not ending with 2 through 5, and *.[!Z] would match strings not ending in .Z.

4.5 Complex Patterns

Korn Shell also provides entire patterns matching.

4.5.1 *(pattern)

This format matches any zero or more occurrences of pattern.

This pattern matches anti, antic, antigen, and antique: $ match anti*(c|gen|que)

4.5.2 ?(pattern)

This format matches any zero or one occurrences of pattern. Here are some more patterns using this format:


4.5.3 +(pattern)

This format matches one or more occurrences of pattern.

4.5.4 @(pattern)

This format matches exactly one occurrence of pattern. Let's look for words beginning with Ala or Cla:     $ match @([AC]la)*

4.5.5 !(pattern)

This format matches anything except pattern. To match any string that does not end in .c, .Z, or .o:
or any string that does not contain digits:

4.6 Disabling File Name Substitution

File name substitution can be disabled by setting the noglob option using the set command:
        $ set —o noglob
        $ set —f

The -o noglob and -f options
for the set command are the same. Once file name substitution is
disabled, pattern-matching characters like *, ?, and ] lose their
special meaning:
        $ ls a*
        a* not found
        $ ls b?
        b? not found
Within [...] patterns, a / character is used to remove the meaning of the special pattern-matching characters. This means that the [/*/?]* pattern would match file names beginning with * or ?.

5 Command Substitution

substitution is a feature that allows output to be expanded from a
command. It can be used to assign the output of a command to a
variable, or to imbed the output of a command within another command.
The format for command substitution is:
where command is executed and the output is substituted for the entire $(command) construct.
For example, to print the current date in a friendly format:
        $ echo The date is $(date)
        The date is Fri Jul 27 10:41:21 PST 1996

5.1 Bourne Shell Compatibility

For compatibility with the Bourne shell, the following format for command substitution can also be used:
        `command `
Using `. . .` command substitution, we could get the names of the files in the current directory like this:
        $ echo `ls` are in this directory
        NEWS asp bin pc are this directory

5.2 Directing File Input

There is also a special form of the $(...) command that is used to substitute the contents of a file. The format for file input substitution is:
This is equivalent to $(cat file) or `cat file`, except that it is faster, because an extra process does not have to be created to execute the cat command.

5.3 Arithmetic Operations

Another form of the $(...) command is used to substitute the output of
arithmetic expressions. The value of an arithmetic expression is
returned when enclosed in double parentheses and preceded with a dollar
Here are a few examples.
        $ echo $((3+5))


        $ echo $((8192*16384%23))


5.4 Tilde Substitution

Tilde substitution is used to substitute the pathname of a user's home
directory for ~user. Words in the command line that start with the
tilde character cause the Korn shell to check the rest of the word up
to a slash. If the tilde character is found alone or is only followed
by a slash, it is replaced with the value of the HOME variable.