Using control-z suspends the process (see the output from stty -a which lists the key stroke under susp). That leaves it running, but in suspended animation (so it is not using any CPU resources). It can be resumed later.

If you want to stop a program permanently, then any of interrupt (often control-c) or quit (often control-\) will stop the process, the latter producing a core dump (unless you've disabled them). You might also use a HUP or TERM signal (or, if really necessary, the KILL signal, but try the other signals first) sent to the process from another terminal; or you could use control-z to suspend the process and then send the death threat from the current terminal, and then bring the (about to die) process back into the foreground (fg).

Note that all key combinations are subject to change via the stty command or equivalents; the defaults may vary from system to system.

Answer from Jonathan Leffler on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › exit-command-in-linux-with-examples
exit command in Linux with Examples - GeeksforGeeks
September 6, 2024 - The simplest use of the 'exit' command is without any parameters. When executed, it will close the current terminal session: After pressing enter, the terminal will simply close.
Discussions

shell - How do I exit or cancel a bad bash command? - Unix & Linux Stack Exchange
Trying with the tidy command you mentioned, Ctrl-D works. ... CTRL+C is good for windows CMD too. ... CTRL+C == terminate the current process, Of course may be the given software handle it and CTRL+D doens't work · Of course , They produce a kernel signal if you want to know more, read : ... Ctrl+D is end-of-file, not directly exit... More on unix.stackexchange.com
🌐 unix.stackexchange.com
August 16, 2012
Keyboard shortcut to exit terminal while a process is running (Ctr+D doesn't work if you're running something out of the terminal)
Try: Ctrl+C, the classic exit command. Close the window. This may not always work, it's just a fallback to pressing Ctrl+C if the running process is trapping that signal. Open a new shell session and kill the running process -- find the PID of the process, then: $ kill -9 (pid) More on reddit.com
🌐 r/linux4noobs
8
3
September 16, 2018
How do i exit the "terminal mode"?
🌐 r/neovim
8
4
October 28, 2022
How do I really exit Terminal?
When you type exit, you're not 'exiting' your terminal emulation program (that's what Terminal.app is), you are telling your shell, which you run inside the terminal to exit. A terminal is essentially a text-based KVM, it would be kinda weird if it just disappeared if you disconnected, wouldn't it? You can generally configure terminal emulators to auto-close when you have no more programs running. This means that if you are inside a bash-shell or a zsh-shell, and you exit out of those, the terminal will detect that and close. More on reddit.com
🌐 r/macsysadmin
7
1
September 26, 2022
🌐
Apple Support
support.apple.com › guide › terminal › open-or-quit-terminal-apd5265185d-f365-44cb-8b09-71a064a42125 › mac
Open or quit Terminal on Mac - Apple Support
Once you open a Terminal window, you can execute commands and run tools. Go to the Terminal app on your Mac. Choose Terminal > Quit Terminal. Go to the Terminal app on your Mac. In the window running the shell process you want to quit, type exit, then press Return.
Find elsewhere
🌐
Robert Elder
blog.robertelder.org › intro-to-exit-command
Intro To 'exit' Command In Linux
August 7, 2024 - I use the 'exit' command to exit from the current shell: · The 'exit' command is typically implemented as a built-in shell command that terminates the current shell process:
🌐
Simple English Wikipedia
simple.wikipedia.org › wiki › Exit_(command)
exit (command) - Simple English Wikipedia, the free encyclopedia
March 12, 2013 - exit is a command on the terminal in many operating systems and scripting languages. It is used to stop all processes that are running in the terminal and close the terminal window.
🌐
Oracle
docs.oracle.com › cd › E19455-01 › 806-1360 › usingtermemulators-36078 › index.html
To Close a Terminal Window (Solaris Common Desktop Environment: User's Guide)
Typing exit at the command line is the preferred method of closing a Terminal window. The other two methods don't terminate any background processes you may have started, which can sometimes cause problems. If you started the Terminal window from a command line, you can stop it by pressing Control+C in the window from which you started it.
🌐
Reddit
reddit.com › r/neovim › how do i exit the "terminal mode"?
How do i exit the "terminal mode"? : r/neovim
October 28, 2022 - You can also exit the terminal by typing in the terminal command line “exit”
🌐
Digi
docs.digi.com › › resources › documentation › digidocs › 90002409 › os › cli-exit-t.htm
Exit the command line interface
August 22, 2023 - At the command prompt, type exit. ... Type q or quit to exit. © 2026 Digi International Inc. All rights reserved.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 7891322674834-Exit-an-active-terminal-command-on-MacOS
Exit an active terminal command on MacOS – IDEs Support (IntelliJ Platform) | JetBrains
Just checked that on mac and Control-C worked for me. It is also listed as a default shortcut in a Keyboard shortcuts in Terminal on Mac. Are you using standard mac terminal or something else?
Top answer
1 of 1
21

In Terminal > Preferences… > Profiles > Shell, the default setting for When the shell exits: is: Don't close the window

This can be changed to: Close the window

Then the exit command will close the window.


Edit to address the edited Question:

If you want to close the window using the exit command you can do as suggested in the first part of this answer, however, if you also want to automatically and gracefully close Terminal as well, as part of your script or in place of the exit command as shown in your question, then you can do the following:

First create a simple AppleScript application using the following example AppleScript code in Script Editor:

tell application "Terminal" to if (busy of windows) does not contain true then quit

Saving it as an application named e.g. QuitTerminal in the main Applications folder.

After saving it, open Terminal and then open e.g. QuitTerminal in order to trigger allowing permissions under System Preferences > Security & Privacy > Privacy

Once this is done you can then use the following at the end of your script or in place of the exit command:

open -a "OuitTerminal"

Example based on command shown in your question:

/Users/harikrishna/Desktop/youtube.sh ; open -a "OuitTerminal"

This will only close Terminal if there are no running processes in any of the windows in Terminal.

Note: If you do not want to see e.g. OuitTerminal show in the Dock, you can modify the application using the following command in Terminal:

defaults write '/Applications/OuitTerminal.app/Contents/Info.plist' LSUIElement -bool yes

If you do not care about any running process you can force close Terminal from the script or command using killall Terminal in place of exit; however this is messy and I do not recommend using it over what's presented above.

🌐
Stack Overflow
stackoverflow.com › questions › 55979322 › is-there-anyway-to-exit-from-a-command-in-terminal
macos - Is there anyway to exit from a command in Terminal? - Stack Overflow
Really depends on the ran command. In python, you call a function: ... Terminal actions are nothing but a bash script or a program itself, in some cases, interruptions like: Ctrl + 'Z' - SIGSTOP Ctrl + 'C' - SIGINT Ctrl + '\' - SIGQUIT Ctrl + 'D' - SIGHUP · Will work just fine, however due to implementations or buffer reading, it won't always work, but usually it's something among those lines like 'quit()', 'exit()' or even '\q' on Postgres.
🌐
Reddit
reddit.com › r/vscode › how do i quit out of terminal after typing code command?
r/vscode on Reddit: How do I quit out of terminal after typing code command?
April 3, 2022 -

Hello, I find it annoying that everytime I type the code command it opens VSCode and I then have to go back, click on the terminal, and type "exit". I'd much rather just have it automatically close whenever I type the command. I tried going into the file for the command and adding "exit" at the end, but that doesn't work. Will you please help me figure this out. Thank you!