Return Values और Exit Codes
return इसके exit status को एक छोटे number के रूप में set करता है, लेकिन असल data (जैसे text या एक computed value) वापस देने के लिए, standard trick है इसे echo करना और caller को वह output capture करने देना।In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
square() {
echo $(( $1 * $1 ))
}
result=$(square 6)
echo "The square is $result"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/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: $?"
Login to try C/C++/Java/PHP code in the editor
returnउपयोग करके एक string या एक बड़ी computed value वापस देने की कोशिश करना;returnसिर्फ 0 से 255 तक एक integer accept करता है और function का exit status set करता है, यह data के लिए एक general-purpose return channel नहीं है।- यह भूल जाना कि अगर एक function में कोई explicit
returnनहीं है, तो इसका exit status सिर्फ इसके चलाए आखिरी command का exit status है -- automatically 0 नहीं। $?के साथ एक function की 'return value' capture करने की कोशिश करना जब इसने असल मेंechoके through result print किया हो; printed output capture करने के लिए आपकोresult=$(my_function)चाहिए, और अगर exit status भी चाहिए तो अलग से$?।
return Nfunction का exit statusN(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उपयोग करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: