09. Korn Shell

Korn Shell

Introduction

  • The Korn shell was developed by David Korn from AT&T Bell Lab
  • Like bash, the Korn shell is based on the older Bourne shell and therefore the Korn shell is similar to bash
  • In this note, we discuss the many similarities and few differences between the Korn and bash shells

Running the Korn Shell

  • The Korn shell can be the default login shell, just like bash is our default login shell on voyager
  • The default Korn shell prompt (stored in PS1) is: $
  • From any shell, to start the Korn shell: ksh
  • To check where the Korn shell is in the system: whereis ksh
    • Normally the Korn shell is at: /bin/ksh
  • If the Korn shell is started as a child process of another shell, to get back to the original shell, use: exit
  • To find the current shell that is running: echo $0
  • If the command line editing is not set for ksh as it is for bash, here are some general tips to re-run previous commands with ksh:
    • history will show the last commands that are run, along with their number in the history file
    • r re-run the last command
    • r number re-run the command with the matching number in the history file

Input / Output

  • As in bash, use read for reading in user input
  • To print to screen, we can use echo as with bash, but most Korn shell scripts use print
    • For example: print current value is $var
  • Just like with any other shell, the Korn shell script also accepts command line arguments and options
  • Command line arguments are accessed through the positional parameters and getopts is also available with ksh
  • We can also use redirection of both input and output of the shell
  • We can also use command substitutions and pipes for input and output

Variables and Expressions

  • User defined variables are created, initialized, unset, and accessed the same way as in bash
  • Likewise, system variables such as PATH, PS1, HOME, etc. are the same as in bash
  • Shell variables (such as $0, $1, $2, $*, $@, $?) are the same
  • Arrays are defined and accessed the same way as in bash
  • Strings and arrays can be manipulated with the same operators, such as ${#array[@]} or ${str/expr/newStr}, and by the same built-in functions such as shift and set
  • Regular expressions can be used for string matching by the shell with the operator =~, just as with bash
  • However, the regex metacharacters are limited to:
    • Anchors: ^ $
    • Single character: .
  • Most of the time, regex in ksh are used in conjunction with egrep, sed, or awk
  • There is a special set of regex that are used with the string operators
  • They are similar to regex used by bash and other utilities, but are in different format

Korn : bash:

*(expr) expr*

+(expr) expr+

?(expr) expr?

@(expr1 | expr2) expr1 | expr2

{n,m}(expr) expr{n,m}

  • For example, using regex with string substitution:

str="cis33a cis33b"

print ${str/+(3)/4} print: cis4a cis33b

Or

classes=(cis33a cis33b cis18a cis18b cis18c)

print ${classes[@]/*([a-z])/} print: 33a 33b 18a 18b 18c

Evaluating Expressions

  • Logical operators: !, &&, || are the same
  • String relational operators: =, !=, <, > are the same
  • Numeric relational operators: -ne, -eq, -lt, -gt, -ge, -le are the same
    • As in bash, to evaluate logical and relational expression, use [[ ]]
  • Arithmetic operators are the same: +, /, *=, ++ …
    • Evaluate arithmetic expression the same way: (( expression ))
    • where the variable names do not need a preceding $
  • As with bash, arithmetic compares can be written as (( expression )) where the compare operators are < , >=, == …
  • File tests are the same way, except for the file exist test
    • -a or -e file exists -s exists and is not empty
    • -d file is a directory -nt file1 is newer than file2
    • -f file is a regular file -ot file1 is older than file2
    • -h file is a link
    • -r have read permission
    • -w have write permission
    • -x have execute permission

Flow Control

  • The if and case statements have the same syntax as in bash
  • The for, while, until loops have the same syntax as in bash
  • Functions can be defined the same way as in bash
  • Functions can also be defined this way:

function fA # start with keyword function, no ( )

{

}

  • The return or exit statement works as in bash
  • As in bash, to check for return or exit status, use: $?
  • As in bash, variables are global by default
  • To make a variable local, use: typeset varName

Signals

  • Signals and signal handling are the same way as in bash
  • The trap command is used to catch signals
  • The difference from bash is when signal handling are reset to default:
    • trap - signalName