Note that this is the same as the original bash implementation, and fixes the terminal even on segfaults.