How to interrupt execution of pasted commands in Linux Mint 22.3 Cinnamon GNOME terminal once a command exits with a code different from `0`? Note that `set -e` does not meet my needs, because it closes the terminal and I don't want a workaround similar to writing a script and executing it.
Apr 28, 8:27pm

In Linux Mint 22.3 (Cinnamon/GNOME Terminal), if you want to stop execution of pasted commands as soon as one fails (exits with a non-zero code), but without closing the terminal or writing a script, you have a few options.


Why set -e Doesn't Work for You

  • set -e (or set -o errexit) causes the shell to exit immediately if any command fails.
  • In an interactive shell, this closes the terminal, which is not what you want.

Best Workaround: Use a Subshell with set -e

You can wrap your pasted commands in a subshell with set -e:

bash
(set -e; command1; command2; command3; ...)
  • This runs all commands in a subshell.
  • If any command fails, the subshell exits, but your main terminal remains open.
  • The remaining commands in the subshell do not execute.

Example:

bash
(set -e; ls /nonexistent; echo "This won't run if ls fails")
  • If ls /nonexistent fails, the echo command is skipped, but your terminal stays open.

Alternative: Use && Between Commands

If you paste commands like this:

bash
command1 && command2 && command3
  • Each command only runs if the previous one succeeds.
  • This is not automatic (you must add && between commands), but it works in an interactive shell.

Why No Perfect Native Solution Exists

  • The shell does not natively support stopping execution of pasted commands on error without either:
    • Exiting the shell (set -e).
    • Using a subshell or script.
  • Pasting commands into the terminal is treated as sequential execution, with no built-in way to halt on error.

Summary Table

set -e
Yes
No
No
(set -e; cmd1; ...)
Yes
Yes
Yes (wrap in subshell)
cmd1 && cmd2
Yes
Yes
Yes (add &&)

Recommendation: Use the subshell approach ((set -e; ...)) for the closest match to your needs. It’s a one-liner, requires no script file, and keeps your terminal open.


Question: Would you like a way to make this even more convenient (e.g., with a shell function or alias)?

Apr 28, 8:27pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.