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?

  • 1
    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 'SO- stop being evil' 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

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

Further reading

|improve this answer|||||

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
|improve this answer|||||
  • 1
    To remove a terminal binding, use ^- or undef as the key, e.g. stty eof under. This is specified in POSIX. – Gilles 'SO- stop being evil' Feb 8 '13 at 22:41
  • @glenn Please see my update. – mtk Feb 12 '13 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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