← Back to Bash Course | Chapter 5: Functions | Lesson 6 of 11

Return Values और Exit Codes

एक function का return इसके exit status को एक छोटे number के रूप में set करता है, लेकिन असल data (जैसे text या एक computed value) वापस देने के लिए, standard trick है इसे echo करना और caller को वह output capture करने देना।
Syntax
bash
function_name() {
  commands
  return status_code   # 0-255
}

function_name
status=$?
result=$(function_name)

return Sets Exit Status, Not Data

return N function को तुरंत रोकता है और इसका exit status N set करता है, जो 0 से 255 के बीच एक integer होना चाहिए। यह success/failure signal करने के लिए है, strings या बड़े numbers जैसी computed values वापस देने के लिए नहीं।

उदाहरण: return Sets Exit Status, Not Data

bash
#!/bin/bash
is_even() {
    if (( $1 % 2 == 0 )); then
        return 0
    else
        return 1
    fi
}
if is_even 4; then
    echo "4 is even"
fi
if ! is_even 7; then
    echo "7 is not even"
fi

Returning Data via echo and Capture

एक string या number जैसी असली computed value वापस देने के लिए, function को इसे echo करवाएं, और caller को command substitution, result=$(function_name), उपयोग करके वह output capture करवाएं।

यह Bash में data return करने का standard idiom है क्योंकि return इसे carry नहीं कर सकता।

उदाहरण: Returning Data via echo and Capture

bash
#!/bin/bash
square() {
    echo $(( $1 * $1 ))
}
result=$(square 6)
echo "The square is $result"

Combining Data Output with a Status Code

एक function अपना computed result caller के capture और उपयोग के लिए echo कर सकता है और अलग से यह signal करने के लिए return उपयोग कर सकता है कि यह succeed हुआ या नहीं, caller को दो independent pieces of information देते हुए।

यह pattern उन functions के लिए आम है जो एक तरीके से fail हो सकते हैं जिसे caller को check करना हो।

उदाहरण: Combining Data Output with a Status Code

bash
#!/bin/bash
divide() {
    if [ "$2" -eq 0 ]; then
        echo "0"
        return 1
    fi
    echo $(( $1 / $2 ))
    return 0
}
result=$(divide 10 2)
status=$?
echo "Result: $result, status: $status"
result=$(divide 10 0)
status=$?
echo "Result: $result, status: $status"

Implicit Exit Status from the Last Command

अगर एक function explicitly return कभी call नहीं करता, इसका exit status सिर्फ वह है जो इसके अंदर आखिरी command ने return किया। इसका मतलब है कि एक function का ending मायने रखता है भले ही कोई explicit return statement न हो।

उदाहरण: Implicit Exit Status from the Last Command

bash
#!/bin/bash
check_file() {
    ls "$1" > /dev/null 2>&1
}
touch example.txt
check_file example.txt
echo "Exit status for existing file: $?"
check_file missing.txt
echo "Exit status for missing file: $?"
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. return उपयोग करके एक string या एक बड़ी computed value वापस देने की कोशिश करना; return सिर्फ 0 से 255 तक एक integer accept करता है और function का exit status set करता है, यह data के लिए एक general-purpose return channel नहीं है।
  2. यह भूल जाना कि अगर एक function में कोई explicit return नहीं है, तो इसका exit status सिर्फ इसके चलाए आखिरी command का exit status है -- automatically 0 नहीं।
  3. $? के साथ एक function की 'return value' capture करने की कोशिश करना जब इसने असल में echo के through result print किया हो; printed output capture करने के लिए आपको result=$(my_function) चाहिए, और अगर exit status भी चाहिए तो अलग से $?।
चैप्टर सारांश
  • return N function का exit status N (0-255) set करता है और तुरंत function execute करना रोक देता है -- यह arbitrary data return नहीं करता।
  • Computed data return करने के लिए, function से value echo (या printf) करें और caller में result=$(function_name args) से capture करें।
  • बिना किसी explicit return के, एक function का exit status उसकी आखिरी executed command का होता है।
  • दोनों combine करना common और idiomatic है: data echo करना और success या failure signal करने के लिए भी return 0/return 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.