Korn Shell Basics
This is an ancient post with no relevance to modern systems.
The Korn shell has been available on SCO systems since Xenix
2.3.4, so you can usually depend on finding it. It's not something
you have to add, like bash or tcsh, and it's usually available on
any modern Unix you are likely to work on. Personally, I prefer
Bash (very similar in features to ksh), but you may not have it
easily available. If that's the case, ksh is better than sh.
If you are using Linux, I'd stick with Bash.
The Korn shell has some neat features that make common tasks
easier. For example, it has command recall and editing, built in
math, and extensive string manipulation. Because of that, some
people write fairly extensive programs using ksh.
I wouldn't. The Korn shell is nice, and has some useful
features, but if you are writing real programs, or anything much
more complex than what you'd type at the shell prompt, Perl is a
far better choice. That doesn't mean ksh is useless; on the
contrary, it is very useful and has some nice features which I will
cover below. But it does mean that I'm not going to bother with the
pattern matching or string handling stuff: Perl does it better.
The simplest way to get started with Korn shell is simply to
set -o vi
PS1='$PWD $ '
I'll explain later how to set up so that you always have a Korn
shell, and how to get the "set -o vi" to happen automatically. For
now, notice that your prompt has been changed to include your
current directory, but nothing else looks any different. Issue some
normal commands, like "lc" or "df -v". Everything works as it
Try a "cd" to somewhere else. Notice that your prompt changes to
reflect the directory you are currently in. Nothing so great so
far, except maybe that setting the prompt to be the current
directory is a little easier than in sh or csh.
Ok, time for something more impressive. Try:
Unless your computer has invented a new way of doing math, you
should have gotten "52" as a response. Isn't that easier than "expr
3 \* 4 + 8 \* 5"? No backslashes, no worrying about spacing. You
can also do this:
Before we go on with math, let's try something else. If you
haven't done the math examples I showed, do them both first. Then
hit the ESC key, then "/", and then type "echo" and press RETURN
(<ESC>/echo<ENTER>). You should see the "echo $ans" on
your screen. Hit "/" again, and the "echo $((3*4+8*5))" pops up.
Now hit "k" (lowercase, please). You should see whatever command
you typed just before that. Hit "j", and you are back at "echo
$((3*4+8*5))" again. "k" moves up toward previous commands, "j"
moves down. All this gets started by hitting ESC. If you have a
line on the screen and press ENTER, the command gets executed. If
you press DEL (or ctrl-C if that's your "intr" character), it
doesn't. In either case, you are back at a normal shell after that,
and "k" or "j" don't work to recall lines.
Before leaving this, let's try one more thing:
for i in a b c d e
After typing this and pressing ENTER, you'll get a ">"
prompt. Go ahead and keep typing, pressing ENTER after each line.
You'll get a ">" prompt after each line but the last. I'll
explain that in a minute. For now, just continue:
After typing "done" and pressing ENTER, you get
on your screen. If you don't already understand the ">"
prompt, think of it is the ksh's (and Bourne sh, too) way of saying
"I understand what you said, but I need more". The shell
understands what "for i in a b c d e" means, and tells you so with
">". As long as you keep typing things it understands, the shell
won't complain. If you typed "doit" instead of "do" on the second
line, you'd get an unfriendly message.
But the point of this was not to explain secondary prompts, or
even "for" loops. Hit "ESC", and then "k", and you should see
for i in a b c d e^Jdo^Jecho $i^Jdone
You can press ENTER to execute this again, or you can press "v"
and something really useful happens: your text gets called up into
vi for editing!
If you don't know vi, check the VI Primer for a painless introduction.
To re-execute after making changes (or not), simply do ":wq", or
to quit without executing, type ":q!". One of the things you could
do while in the editor is type ":w /tmp/this_is_it" to put your
little program into a file for later reuse.
The "vi" editing works even without pressing "v". If you recall
a command, you can use space to move to the right, "x" to delete a
character, "a" to append, and so on. You should see "man ksh" for
the full details, but one more useful hint is that you don't have
to press ENTER and recall a line to fix a mistake. As soon as you
realize the error, hit ESC and you are in editing mode.
The ksh can also use emacs style editing if you prefer. You'd
set that with "set -o emacs". Although I personally much prefer the
power of vi, the emacs mode is probably more intuitive for the
inexperienced user. Use CTRL-P and CTRL-N to move through the
stack, CTR-R to search. See the "man ksh" for more details.
Now back to math again. The Korn shell has only integer math,
but it does have modulo division:
# echo $((52 % 3))
and bit operators (left and right shift, AND,OR, Exclusive OR).
These have some use at the command line for quick sanity checks on
masks, for example. What I find even more useful is that ksh can
handle base conversions:
typeset -i2 a
typeset -i8 b
typeset -i16 c
typeset -i36 d
echo $a $b $c $d
2#11111110 8#376 16#fe 36#72
Conversely, you can do:
typeset -i10 e
and get 227. This sort of thing is very useful at the command
line. Of course, things like:
echo $((8#376 / 2))
work as they should, also.
You might be wondering now how to set up so that you are always
using Korn shell. There are a couple of ways to do that. One is to
have set yourself up that way to begin with; to have specified ksh
when your account was created. If that wasn't done, it's not too
If you can change your account using "usermod" or "scoadmin" or
"sysadmsh" (on older systems) or "chsh" (Linux, BSD), do so. That will get you going with
ksh as your default shell. However, you still have more to do. If
you can't change your default shell (you aren't the administrator,
for example), you can still get 99% of the work done
First, you need to add this to the end of your .profile:
if [ -f $HOME/.kshrc -a -r $HOME/.kshrc ]; then
ENV=$HOME/.kshrc # set ENV if there is an rc file
Second, you need to create a $HOME/.kshrc.
Use vi, or:
cat > $HOME/.kshrc
if [ -z "$VISUAL" -a -z "$EDITOR" ]; then
set -o vi
PS1='$PWD $ '
(type control-D to end the cat)
If you were able to change your default shell, you now have
everything you need. If not, just add:
to the end of your .profile (after the ENV lines you already
Or, simply type "ksh" when you want it. Because "ENV" has been
exported, the shell will read the settings in .kshrc and everything
will work as you want it.
Command line recall
Add these to your .kshrc:
set -o emacs
alias __A=$(print '\0020') # ^P = up = previous command
alias __B=$(print '\0016') # ^N = down = next command
alias __C=$(print '\0006') # ^F = right = forward a character
alias __D=$(print '\0002') # ^B = left = back a character
alias __H=$(print '\0001') # ^A = home = beginning of line
If this page was useful to you, please help others find it:
More Articles by Tony Lawrence
- Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site:
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Publishing your articles here
Jump to Comments
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.