Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make the terminal autoclose when a command runs successfully. It shouldn't autoclose if the command produces some error.

For example:

If I run sudo apt-get install vlc, the terminal should be closed automatically if the VLC package installs successfully. Otherwise, it shouldn't close.

share|improve this question
up vote 3 down vote accepted

use the following command

bind 'RETURN: ";if [[ $? == 0 ]]; then exit; fi;\n"'

if you need it work everytime when you start new session, add this command to ~/.bashrc file, and then run source ~/.bashrc to make it take effect immediately.

then if you enter


it will be automatically replaced by

ls; if [[ $? == 0 ]]; then exit; fi;
share|improve this answer
where i have to use bind 'RETURN: ";if [[ $? == 0 ]]; then exit; fi;\n"' command? – Avinash Raj Feb 9 '14 at 5:20
@AvinashRaj Just run this command in your terminal. If you would like it work everwhere, put it into .bashrc. – House Zet Feb 9 '14 at 5:24
i want it to work for all the commands. – Avinash Raj Feb 9 '14 at 5:28
It should. Have you tried it?(put this command in .bashrc will make it take effect for all command) – House Zet Feb 9 '14 at 5:29
pls edit your answer for adding the above command to .bashrc and source it to work. – Avinash Raj Feb 9 '14 at 5:40

Just put && exit after the command you want to quit the terminal:


sudo apt-get install vlc && exit


&&      Run the next command if the preceding command exits with 0 (success)
exit    Exit the terminal or script

This is probably better than exiting if any command succeeds; you probably want to run more than one command in a terminal.

If you're really sure the terminal should exit after every successful command, use this in your .bashrc:

bind 'RETURN: "\C-e ; [[ $? == 0 ]] && exit ;

(ensure that the closing quotes are on a new line)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.