How to Stop the Ubuntu ‘Terminal’ from Automatically Closing When a Command Finishes

Why Does the Terminal Close Automatically?

When you create a custom desktop shortcut or run a shell script by double-clicking it in Ubuntu, the system often opens a new terminal window to execute the command. However, the moment the command finishes—whether it succeeds or fails—the terminal window instantly closes.

This behavior is incredibly frustrating when you are trying to debug a script. If there is an error message outputted to the screen, the window vanishes before you can read it. To troubleshoot effectively, you need the terminal to stay open after the command executes.

How to Keep the Ubuntu Terminal Open After Execution

You can solve this problem by modifying the default profile settings within the GNOME Terminal application.

  1. Open your Ubuntu Terminal (shortcut: Ctrl + Alt + T).
  2. In the top-right corner of the terminal window, click the Hamburger menu (three horizontal lines).
  3. Select Preferences from the dropdown menu.
  4. In the left sidebar, under the “Profiles” section, click on your current profile (usually named Unnamed or default).
  5. Click on the Command tab at the top of the main panel.
  6. Look for the dropdown menu labelled When command exits:.
  7. By default, it is set to “Exit the terminal.” Click the menu and change it to Hold the terminal open.
  8. Close the Preferences window.

Now, when you run a custom script or a one-off command that launches a new terminal window, the window will execute the code and simply wait at the prompt when finished. This allows you to easily read the output logs, view any error messages, and manually close the window (by clicking the X) when you are ready.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.