How to stop iTerm2 closing tabs on error? [duplicate]

In Terminal, the default setting for handling closed sessions is to leave the window open.

In Preferences > Profiles:

enter image description here

In iTerm2, the default is to close the window

enter image description here

If you disable that option in iTerm2, it should give you the behavior you're looking for.


The section Scripting of the iTerm2 documentation helped me fixing my problem.

In .zshrc I defined the following function:

function exit {
    osascript ~/.zsh/.function_exit.as
}

With ~/.zsh/.function_exit.as as

tell application "iTerm2"
    tell current window to close current session
end tell

Additionally I set the following option in the iTerm2 settings:

enter image description here

enter image description here

Using all the above things together I achieve the following behaviour:

enter image description here

Since this does not depend on the Shell used everything should work perfectly using bash (and the corresponding dot-file).