← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 2 of 7

Script Header के रूप में set -euo pipefail

एक script के top के पास एक line जोड़ना एक साथ कई safety checks on कर देता है, Bash को silently आगे बढ़ने के बजाय कुछ गलत दिखने पर तुरंत रुकने और complain करने देते हुए।
Syntax
bash
#!/bin/bash
set -euo pipefail

Combined Header

set -euo pipefail एक साथ तीन protections enable करने का एक compact तरीका है: एक unhandled command failure पर तुरंत exit करें, एक unset variable reference करने पर error दें, और एक pipeline का exit status किसी भी failing stage को reflect करे, सिर्फ last को नहीं।

उदाहरण: The Combined Header

bash
#!/bin/bash
set -euo pipefail

name="demo script"
echo "running $name safely"

pipefail का Effect देखना

pipefail के बिना, एक pipeline में एक failing first stage एक successful later stage से mask हो जाता है। इसे enable करने पर, pipeline का exit status इसके सभी stages में से पहले non-zero status बन जाता है।

उदाहरण: Seeing pipefail's Effect

bash
#!/bin/bash
set -o pipefail

if false | true; then
    echo "unreachable with pipefail active"
else
    echo "pipefail correctly noticed the earlier failure"
fi

Strict Mode को Incrementally Testing करना

एक existing script पर set -euo pipefail retrofit करते समय, flags को एक समय में एक enable करना (या एक subshell में test करना) मदद करता है ताकि कोई newly surfaced failure एक specific flag से trace की जा सके, सभी तीन से नहीं।

उदाहरण: Testing Strict Mode Incrementally

bash
#!/bin/bash
(
    set -u
    echo "testing just -u in isolation"
)
(
    set -e
    echo "testing just -e in isolation"
)
echo "both isolated tests completed without crashing the main script"

एक Realistic Script Header

Practice में, set -euo pipefail shebang line के ठीक बाद placed है, किसी भी और logic से पहले, ताकि पूरी script इन protections का benefit पहले command से ही ले।

उदाहरण: A Realistic Script Header

bash
#!/bin/bash
set -euo pipefail

process_item() {
    local item=$1
    echo "processing: $item"
}

for item in alpha beta gamma; do
    process_item "$item"
done
echo "all items processed successfully"
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. एक existing बड़ी script में set -euo pipefail जोड़ना और surprised होना जब यह अचानक "टूट जाती" है, जब असल में यह उन bugs (unset variables, ignored failures) को expose कर रही है जो पहले से वहां थे।
  2. यह भूल जाना कि set -e की exemptions (if/while conditions के अंदर, pipefail के बिना non-last pipeline stages) combined flags के साथ भी apply होती हैं।
  3. set -euo pipefail के under scripts incrementally test न करना, यह बताना hard बनाते हुए कि तीन flags में से किस कारण किसी failure का कारण बना।
चैप्टर सारांश
  • set -euo pipefail exit-on-error (-e), error-on-unset-variable (-u), और pipeline-failure-propagation (-o pipefail) को एक line में combine करता है।
  • यह non-trivial bash scripts के लिए एक widely recommended defensive header है, भले ही यह हर possible bug न पकड़े।
  • इसे एक pre-existing script में जोड़ना आमतौर पर नए bugs introduce करने के बजाय पहले से silent bugs surface करता है।
  • इसके बावजूद उन specific failures के लिए explicit error handling worth है जिनकी आपको सबसे ज़्यादा चिंता है, सिर्फ strict mode पर rely करने के बजाय।

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.