← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 12 of 12

Exit Status और $?

Bash में finish होने वाला हर command exit status नाम का एक छोटा number वापस report करता है, जहां 0 का मतलब success है और कुछ भी और किसी तरह की failure का मतलब है।
Syntax
bash
command
echo "$?"    # 0 = success, non-zero = failure

command1 && command2
command1 || command2

Command के बाद $? पढ़ना

किसी भी command के finish होने के बाद, Bash इसका exit status special variable $? में store करता है। आप immediately बाद में इसे inspect करके decide कर सकते हैं कि आपकी script को आगे क्या करना चाहिए, लेकिन अगला command run होते ही यह overwrite हो जाता है।

उदाहरण: Reading $? After a Command

bash
#!/bin/bash
echo "hello"
echo "Exit status of echo: $?"
grep "zzz" <<< "no match here"
echo "Exit status of grep (1 = no match found): $?"

Success बनाम Failure Codes

Exit status 0 conventionally मतलब है कि command succeed हुआ, जबकि 1 से 255 तक की कोई भी value failure signal करती है, भले ही हर non-zero number का exact meaning उस specific command द्वारा defined हो।

बहुत से commands सिर्फ 0 या 1 return करते हैं, लेकिन कुछ different तरह के errors distinguish करने के लिए अलग numbers उपयोग करते हैं।

उदाहरण: Success vs Failure Codes

bash
#!/bin/bash
true
echo "true's exit status: $?"
false
echo "false's exit status: $?"

Exit Status के आधार पर && और ||

&& एक दूसरे command को सिर्फ तब run होने के लिए chain करता है जब पहला status 0 (success) के साथ exit हुआ हो। || इसका opposite करता है, दूसरे command को सिर्फ तब run करता है जब पहला non-zero (failure) exit हुआ हो, जो simple conditional logic के लिए एक आम shorthand है।

उदाहरण: && and || Based on Exit Status

bash
#!/bin/bash
mkdir -p /tmp/demo_$$ && echo "Directory created successfully"
grep "missing" <<< "nothing to find" || echo "Pattern not found, handled gracefully"

आपकी Script का अपना Exit Status Set करना

एक script या function exit builtin के साथ एक number देकर explicitly वह exit status set कर सकता है जो callers देखेंगे। अगर exit को बिना argument के call किया जाए, script उस आखिरी command के status के साथ exit होती है जो इसने run किया था।

उदाहरण: Setting Your Script's Own Exit Status

bash
#!/bin/bash
check_value=5
if [ "$check_value" -gt 0 ]; then
    echo "Value is positive"
    exit 0
fi
echo "This line would only run for non-positive values"
exit 1
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. यह मान लेना कि एक non-zero exit status हमेशा एक crash का मतलब है; यह आमतौर पर सिर्फ command की failure की अपनी definition का मतलब है, जैसे grep सिर्फ तब 1 return करता है जब इसे कोई matches न मिलें, जो कोई error condition नहीं है।
  2. बीच में एक unrelated command, जैसे debugging के लिए echo, run करने के बाद $? check करना; $? हमेशा *सबसे recently* completed command को reflect करता है, इसलिए आपके care वाले command के बाद run होने वाला कोई भी command इसे overwrite कर देता है।
  3. यह भूल जाना कि exit status 0-255 पर capped है; बड़ी values इसके store होने के तरीके की वजह से wrap around होती हैं, जो surprising results produce कर सकता है।
चैप्टर सारांश
  • $? सबसे recently executed command का exit status रखता है।
  • Convention के अनुसार, exit status 0 मतलब success है, और कोई भी non-zero value (1-255) मतलब failure या एक specific error condition है।
  • && अगला command सिर्फ तब run करता है जब पिछला 0 exit हुआ हो; || अगला command सिर्फ तब run करता है जब पिछला non-zero exit हुआ हो।
  • एक script का अपना exit status explicitly exit builtin से set किया जा सकता है, जैसे exit 1।

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.