And does Korn shell have an equivalent to PROMPT_COMMAND?

In Bash, I'm able to make function calls with a parameter when a single input word matches a regular expression. Can Korn shell do this?

Let's say ksh88.

For example, in Bash, I can call a function by entering any single word in the command line matching a regular expression by doing:


    if [[ $1 =~ ^[A-Z]+\.[0-4]+$ ]]; then
        callFunction "$1"
        return 222;
        printf "command not found: %s\n" "$1" >&2
        return 127


PROMPT_COMMAND='[[ $? -eq 222 ]] && callFunction'

Can something similar be done in ksh88?

  • 1
    What "KornShell"? ksh93, pdksh & co (mksh, OpenBSD's ksh), the real ksh88, zsh masquerading as ksh?
    – mosvy
    Apr 6 '20 at 14:20
  • It seems you've asked two (related?) questions: one about a ksh PROMPT_COMMAND and another about calling a function based on the first parameter. Are they tightly related or are these two separate questions?
    – Jeff Schaller
    Apr 6 '20 at 14:22
  • For the PROMPT_COMMAND, you can simply put a command substitution in PS1: PS1='$(echo Beware, the prompt is going to be printed!! >&2)the Ksh$ '
    – mosvy
    Apr 6 '20 at 14:28
  • 1
    @mosvy, or use ${ cmd; } (in ksh93 / ksh2020) to avoid the subshell, or set the PS1.get discipline. For command_not_found_handler, you can probably use a DEBUG trap. Apr 6 '20 at 14:33
  • 1
    I don't have any historical ksh88 to check how its DEBUG trap worked, but AFAIK pdksh and derivatives don't support the DEBUG trap. Also, your bash trick looks broken, what will happen when the a command like E.1 is run from a pipeline or command substitution?
    – mosvy
    Apr 6 '20 at 14:48

ksh93 can also support PROMPT_COMMAND with a discipline function (via dgk).

function PS1.get 
    integer exitval=$?
    eval .sh.value=$($PROMPT_COMMAND)'$PS1'
    return $exitval

ksh88 already has something better than that bash method.

There is a special trap condition called ERR. So try this:

trap 'echo bla' ERR

and you see that echo bla is executed.

BTW: This also works with bash...

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.