Automatically exit when bash command produce return code non zero [duplicate]












0
















This question already has an answer here:




  • Automatic exit from bash shell script on error

    7 answers



  • Error handling in Bash

    14 answers




In bash how do we make the script to automatically exit if a command line return code is not zero. For example:



#!/bin/bash

cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"


I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.



And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command










share|improve this question













marked as duplicate by Charles Duffy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    You can use set -e or #!/bin/bash -e

    – anubhava
    Nov 26 '18 at 18:35











  • ^^ + Otherwise known as the "Error Exit" flag

    – Zak
    Nov 26 '18 at 18:37











  • mv file_a /somedir/file_a || exit?

    – Cyrus
    Nov 26 '18 at 18:37











  • @anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

    – RonPringadi
    Nov 26 '18 at 18:39











  • Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

    – Charles Duffy
    Nov 26 '18 at 18:39


















0
















This question already has an answer here:




  • Automatic exit from bash shell script on error

    7 answers



  • Error handling in Bash

    14 answers




In bash how do we make the script to automatically exit if a command line return code is not zero. For example:



#!/bin/bash

cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"


I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.



And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command










share|improve this question













marked as duplicate by Charles Duffy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    You can use set -e or #!/bin/bash -e

    – anubhava
    Nov 26 '18 at 18:35











  • ^^ + Otherwise known as the "Error Exit" flag

    – Zak
    Nov 26 '18 at 18:37











  • mv file_a /somedir/file_a || exit?

    – Cyrus
    Nov 26 '18 at 18:37











  • @anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

    – RonPringadi
    Nov 26 '18 at 18:39











  • Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

    – Charles Duffy
    Nov 26 '18 at 18:39
















0












0








0









This question already has an answer here:




  • Automatic exit from bash shell script on error

    7 answers



  • Error handling in Bash

    14 answers




In bash how do we make the script to automatically exit if a command line return code is not zero. For example:



#!/bin/bash

cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"


I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.



And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command










share|improve this question















This question already has an answer here:




  • Automatic exit from bash shell script on error

    7 answers



  • Error handling in Bash

    14 answers




In bash how do we make the script to automatically exit if a command line return code is not zero. For example:



#!/bin/bash

cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"


I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.



And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command





This question already has an answer here:




  • Automatic exit from bash shell script on error

    7 answers



  • Error handling in Bash

    14 answers








bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 18:33









RonPringadiRonPringadi

342114




342114




marked as duplicate by Charles Duffy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Charles Duffy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    You can use set -e or #!/bin/bash -e

    – anubhava
    Nov 26 '18 at 18:35











  • ^^ + Otherwise known as the "Error Exit" flag

    – Zak
    Nov 26 '18 at 18:37











  • mv file_a /somedir/file_a || exit?

    – Cyrus
    Nov 26 '18 at 18:37











  • @anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

    – RonPringadi
    Nov 26 '18 at 18:39











  • Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

    – Charles Duffy
    Nov 26 '18 at 18:39
















  • 3





    You can use set -e or #!/bin/bash -e

    – anubhava
    Nov 26 '18 at 18:35











  • ^^ + Otherwise known as the "Error Exit" flag

    – Zak
    Nov 26 '18 at 18:37











  • mv file_a /somedir/file_a || exit?

    – Cyrus
    Nov 26 '18 at 18:37











  • @anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

    – RonPringadi
    Nov 26 '18 at 18:39











  • Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

    – Charles Duffy
    Nov 26 '18 at 18:39










3




3





You can use set -e or #!/bin/bash -e

– anubhava
Nov 26 '18 at 18:35





You can use set -e or #!/bin/bash -e

– anubhava
Nov 26 '18 at 18:35













^^ + Otherwise known as the "Error Exit" flag

– Zak
Nov 26 '18 at 18:37





^^ + Otherwise known as the "Error Exit" flag

– Zak
Nov 26 '18 at 18:37













mv file_a /somedir/file_a || exit?

– Cyrus
Nov 26 '18 at 18:37





mv file_a /somedir/file_a || exit?

– Cyrus
Nov 26 '18 at 18:37













@anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

– RonPringadi
Nov 26 '18 at 18:39





@anubhava thanks that's what I need. Strangely my bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?

– RonPringadi
Nov 26 '18 at 18:39













Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

– Charles Duffy
Nov 26 '18 at 18:39







Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse, set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.

– Charles Duffy
Nov 26 '18 at 18:39














1 Answer
1






active

oldest

votes


















2














There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.



if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.



    if cd /something_something && mv file_a /somedir/file_a; then
    echo $?
    exit
    fi
    echo "Both cd and mv worked"





    share|improve this answer




























      2














      There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.



      if cd /something_something && mv file_a /somedir/file_a; then
      echo $?
      exit
      fi
      echo "Both cd and mv worked"





      share|improve this answer


























        2












        2








        2







        There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.



        if cd /something_something && mv file_a /somedir/file_a; then
        echo $?
        exit
        fi
        echo "Both cd and mv worked"





        share|improve this answer













        There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.



        if cd /something_something && mv file_a /somedir/file_a; then
        echo $?
        exit
        fi
        echo "Both cd and mv worked"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 18:39









        chepnerchepner

        251k33236331




        251k33236331

















            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista