Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

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

ls

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 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 at 5:24
    
i want it to work for all the commands. –  Avinash Raj Feb 9 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 at 5:29
    
pls edit your answer for adding the above command to .bashrc and source it to work. –  Avinash Raj Feb 9 at 5:40

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

Example:

sudo apt-get install vlc && exit

Explanation:

&&      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

 
discard

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.