Is there a command to close a terminal window via commandline?
10 Answers
To close a terminal window you can use the exit
command . Alternatively you can use the shortcut ctrl+shift+w to close a terminal tab and ctrl+shift+q to close the entire terminal including all tabs.
You can use the ^D
shortcut - that is, hitting Control and d. This works in many shells and shell-like environments. Technically speaking, this key combination emits the end-of-transmission character (ASCII code 04). In gnome-terminal
, i.e. bash
and other shells, this is interpreted as finishing the interactive session. You can also see this behavior in programs like cat
which read from standard input (the keyboard). Try running it without arguments; you can terminate by entering ^D
.
There is an alternate way, which requires a bit more keystrokes, but is worth knowing.
Using exec
command
- Syntax
exec <command-name>
This will first execute the
<command-name>
and then exit the shell.For example, you could do:
exec sleep 1
which would, effectively, close the window after 1 second.
-
It doesn't quite "exit" the shell, but it rather replaces the shell's process with the
<command-name>
. When command terminates, process exits. Nov 30, 2015 at 11:46 -
Exactly what I was looking for. Running python scripts with bash and killing them later– JoshJul 25, 2019 at 16:20
I know this answer is a little bit late but, If you are using Ubuntu or another Linux distro, simply use Ctrl+D to close the entire terminal.
I initially tried all the rest and they were just not working. For example I see people recommending exit
but honestly, it just doesnt work.
I hope someone agrees with me.
-
2Ctrl + D works indeed, but in my case "exit" has always been working too... It's weird that it doesn't for you.– FloTNov 7, 2019 at 14:03
You can use xdotool
:
xdotool getactivewindow windowkill
Notice that this will kill all active windows!
If you want to kill only the current window use:
xdotool key --clearmodifiers Ctrl+Shift+Q key --clearmodifiers KP_Enter
-
Welcome to AskUbuntu. A good answer that can be improved. "Try using" is the hallmark of a suggestion and suggestions should go in comments. Replacing it with "You can use" makes it into a proper answer. I'll suggest an editing.– user589808Oct 18, 2016 at 10:11
-
-
Yes there is.First type echo $$
in terminal to know it's process id. Then type kill -9 processid
.
To close the terminal whilst leaving all running processes to continue running, you can do the following:
- pause the process using Ctrl + Z , pull it into the background with
bg
and then disown it, eg:
vlc
^Z
[1]+ Stopped
bg
[1]+ vlc & disown
exit
This will leave the current running process running in the background and close the terminal(i find this to be very useful).
I'm using KDE here. I wrote a script and I execute it by right click > execute in console. After the executtion, the newly opened window of konsole, was left open and I had to manualy close it (by pressing X or Alt+F4, or typing exit). I combined two of your answers here (THANKS to the posters) and now the window closes automatically.
Sample:
#!/bin/bash
#
echo Hello & disown
sleep 1
echo Closing in 3
sleep 1
echo Closing in 2
sleep 1
echo Closing in 1
sleep 1
xdotool getactivewindow windowkill
Without the & disown and xdotool getactivewindow windowkill, just by using "exit" at the end, nothing happened! Thank you again!
-
I noticed that while the above script is counting down, if I activate another window, par example, dolphin file manager, then the activated window will close, instead of konsole's window!– jimmyzDec 1, 2022 at 7:53