← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 4 of 12

set -e (Error पर Exit) और इसकी Gotchas

set -e एक script को बताता है कि किसी भी command के fail होते ही immediately रुक जाए, बजाय ऐसे continue करने के जैसे कुछ हुआ ही न हो।
Syntax
bash
set -e
command_that_must_succeed
command_that_may_fail || true

Basic Exit-on-Error Behavior

set -e active होने पर, एक script चलना बंद कर देता है जैसे ही कोई command एक non-zero exit status return करे, बजाय silently बाद वाली lines में continue करने के जो उस command के succeed होने पर depend कर सकती हैं।

उदाहरण: Basic Exit-on-Error Behavior

bash
#!/bin/bash
(
    set -e
    echo "step 1"
    false
    echo "step 2 - never reached"
) || echo "the subshell stopped early because of set -e"

The if-condition Exemption

एक command जिसे if, while, या until की condition के रूप में test किया जा रहा है set -e से exempt है, क्योंकि वहां एक non-zero status expected और meaningful है, एक bug नहीं।

उदाहरण: The if-condition Exemption

bash
#!/bin/bash
(
    set -e
    if false; then
        echo "unreachable"
    else
        echo "set -e did NOT trigger here, because false was an if-condition"
    fi
    echo "the subshell kept running normally"
)

The Pipeline Exemption

एक pipeline के अंदर, plain set -e सिर्फ LAST command के exit status पर react करता है, इसलिए एक earlier stage में failure silently ignore हो जाती है जब तक pipefail भी enabled न हो।

उदाहरण: The Pipeline Exemption

bash
#!/bin/bash
(
    set -e
    false | true
    echo "reached because only the last command (true) mattered to set -e"
)

(
    set -e -o pipefail
    false | true
    echo "unreachable with pipefail active"
) || echo "with pipefail, the earlier failure was noticed and stopped the subshell"

Catching a set -e Failure Deliberately

एक risky command को अपने खुद के set -e वाले एक subshell में wrap करना, और subshell के overall result को || से check करना, एक script को पूरे script को kill किए बिना एक strict failure demonstrate या contain करने देता है।

Note: set -e behavior को एक large script पर भरोसा करने से पहले एक छोटे subshell में ऐसे test करें; इसकी exemptions trip करना आसान है।

उदाहरण: Catching a set -e Failure Deliberately

bash
#!/bin/bash
(set -e; false; echo "unreached") || echo "caught: the previous command failed under set -e"
echo "the main script keeps running normally"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि set -e हर possible failure catch करता है; यह एक if/while condition के अंदर, एक &&/|| से पहले, या एक pipeline के किसी भी-but-last part के रूप में एक failing command के लिए trigger नहीं होता।
  2. यह देखकर surprised होना कि एक condition में उपयोग किए गए एक function call के अंदर एक failing command के बाद भी script चलता रहता है, क्योंकि set -e की exemptions वहां भी apply होती हैं।
  3. Robust error handling के लिए सिर्फ set -e पर भरोसा करना बजाय इसे उन specific commands के लिए explicit checks के साथ combine करने के जो सबसे ज़्यादा matter करते हैं।
चैप्टर सारांश
  • set -e script को तुरंत exit करता है अगर एक command non-zero exit हो, next line पर continue करने के बजाय।
  • set -e if, while, until, या &&/|| से पहले test की गई एक command पर APPLY नहीं होता — उनके potentially fail होने की expectation होती है।
  • एक pipeline में, set -e सिर्फ LAST command के exit status पर react करता है, जब तक pipefail भी set न हो।
  • set -e को ज़्यादा predictable strict-mode behavior के लिए set -u और set -o pipefail के साथ combine करना best है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.