To help me identify whether a command fails and what is its exit code, I trap the ERR special signal to print the exit code of a command that returns nonzero.
Code:
trap 'printf "\e[1;31mEXIT: %s\e[0m\n" "$?"' ERR
You can try it in your shell.
Run this and then run `false` or `cat nonexistent/file.txt`.
However, OpenBSD pdksh behaves a little different from ksh93 and bash when the command terminates from a signal.
For example. Try this in bash(1) or ksh93(1): run the trap command and then run `cat` (without any arguments) and immediately type `^C` (ctrl+c) to send `SIGINT` to cat. The shell will print `EXIT: 130` (or `EXIT: 258`) right after the command is terminated and before the next prompt.
Try this again in OpenBSD ksh(1). The `EXIT: 130` string will not be printed immediately. Now enter a successful command, such as grep(1) or ls(1). NOW the error string will be printed.
This image illustrates what I am saying:
- First, I'm on a ksh93(1) subshell.
I run `false`, and it prints `EXIT: 1`.
I run `cat` and type `^C` and it prints `EXIT 258`.
I run `grep -V` and it prints nothing.
- Then, I exit ksh93(1) and return to ksh(1).
I run `false`, and it prints `EXIT: 1`.
I run `cat` and type `^C` and it prints nothing.
I run `grep -V` and it prints `EXIT 130`.
Is this a bug? If yes, is it known by the developers?
I think that it can be a problem in a script to not handle a ERR trap right after a command terminates from a signal.