Script Header के रूप में set -euo pipefail
In this page:
#!/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
#!/bin/bash
set -euo pipefail
name="demo script"
echo "running $name safely"
Login to try C/C++/Java/PHP code in the editor
pipefail का Effect देखना
pipefail के बिना, एक pipeline में एक failing first stage एक successful later stage से mask हो जाता है। इसे enable करने पर, pipeline का exit status इसके सभी stages में से पहले non-zero status बन जाता है।
उदाहरण: Seeing pipefail's Effect
#!/bin/bash
set -o pipefail
if false | true; then
echo "unreachable with pipefail active"
else
echo "pipefail correctly noticed the earlier failure"
fi
Login to try C/C++/Java/PHP code in the editor
Strict Mode को Incrementally Testing करना
एक existing script पर set -euo pipefail retrofit करते समय, flags को एक समय में एक enable करना (या एक subshell में test करना) मदद करता है ताकि कोई newly surfaced failure एक specific flag से trace की जा सके, सभी तीन से नहीं।
उदाहरण: Testing Strict Mode Incrementally
#!/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"
Login to try C/C++/Java/PHP code in the editor
एक Realistic Script Header
Practice में, set -euo pipefail shebang line के ठीक बाद placed है, किसी भी और logic से पहले, ताकि पूरी script इन protections का benefit पहले command से ही ले।
उदाहरण: A Realistic Script Header
#!/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"
Login to try C/C++/Java/PHP code in the editor
- एक existing बड़ी script में
set -euo pipefailजोड़ना और surprised होना जब यह अचानक "टूट जाती" है, जब असल में यह उन bugs (unset variables, ignored failures) को expose कर रही है जो पहले से वहां थे। - यह भूल जाना कि
set -eकी exemptions (if/whileconditions के अंदर,pipefailके बिना non-last pipeline stages) combined flags के साथ भी apply होती हैं। set -euo pipefailके under scripts incrementally test न करना, यह बताना hard बनाते हुए कि तीन flags में से किस कारण किसी failure का कारण बना।
set -euo pipefailexit-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 करने के बजाय।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script