Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to know how we can map the keys Ctrl-D in ksh to exit the shell as it does in bash shell.

Not much familiar with key binding in ksh.


See my comments below.

$ echo $0
$ exit    # I pressed Ctrl-D here, 'exit' string was placed and the shell exited

$> echo $0
$> Use 'exit' to leave ksh  # I pressed Ctrl-D here, Got this message and returned to the ksh prompt
$> echo $0
$> cat >> somefile
Some text
$>   # I pressed Ctrl-D here. So this key combination is taken as eof char for file, but not for exit.

So, it's needs Ctrl-D to be mapped to exit the shell? How do I do that?

share|improve this question
If you enter ^D after some characters, it isn't heeded as "end of file". It must go on a line on its own. – vonbrand Feb 8 '13 at 14:03
Ctrl+D should work in the same way in bash and ksh. What different behavior do you observe? – Gilles Feb 8 '13 at 22:42
@vonbrand Please see my update. – mtk Feb 12 '13 at 8:41
It works the same. Just that bash writes exit when exiting, ksh doesn't. – vonbrand Feb 12 '13 at 12:00
@vonbrand No, it isn't exiting the ksh. That's what I showed. Please check. I am still on ksh shell. It has not exitted. – mtk Feb 12 '13 at 12:08

You want the stty command: see how ^D is mapped with stty -a. You can change that to something else with stty eof char: Remove the "eof" setting with stty eof undef

Untested: you want to read about the trap command in the ksh man page. It might be sufficient for you to set up a handler for the EXIT signal.

# warning: completely untested
exit_handler() { echo "exit"; exit; }
trap exit_handler EXIT
share|improve this answer
To remove a terminal binding, use ^- or undef as the key, e.g. stty eof under. This is specified in POSIX. – Gilles Feb 8 '13 at 22:41
Ah, thanks Gilles. – glenn jackman Feb 8 '13 at 23:43
@glenn Please see my update. – mtk Feb 12 '13 at 8:43

Ctrl+D (ASCII EOT) already is mapped to exiting the shell, in both vi and emacs modes, as the ksh93 manual page explains. It's quite evidently so from your script capture.

The manual page also explains what others have commented here, that this key only has this effect if the line is empty. (This mimics the standard behaviour of the line discipline in cooked mode.)

What you are missing, which is also explained by the manual page, is that you have the ignoreeof option set on. So the Korn Shell has seen the Ctrl+D on an empty line, and ignored it, printing the very message that you see, to explain that with ignoreeof set on you must explicitly use the exit command.

So forget about stty and trap handlers, as in the other answer; and simply:

set -o noignoreeof

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.