Functions Share करने के लिए Files Source करना
source command (या इसका shorthand .) एक दूसरी script के commands को सीधे आपके current shell के अंदर run करता है, जो multiple script files के बीच functions और variables share करने का तरीका है।In this page:
source library_file.sh
. library_file.sh
function_from_library arguments
Functions Import करने के लिए एक File Source करना
source filename (या equivalent shorthand . filename) किसी और file के commands को सीधे current shell के अंदर execute करता है, इसलिए यह जो भी functions या variables define करती है वे बाद में calling script में available हो जाते हैं।
Shared logic को एक reusable library file में split करने का यह standard mechanism है।
उदाहरण: Sourcing a File to Import Functions
#!/bin/bash
cat <<'EOF' > mathlib.sh
add() {
echo $(( $1 + $2 ))
}
multiply() {
echo $(( $1 * $2 ))
}
EOF
source mathlib.sh
echo "3 + 4 = $(add 3 4)"
echo "3 * 4 = $(multiply 3 4)"
Login to try C/C++/Java/PHP code in the editor
The . Shorthand
Single dot . source का एक POSIX-standard shorthand है और identically behave करता है; . ./mathlib.sh ठीक source ./mathlib.sh के equivalent है।
Dot form Bash के अलावा दूसरी shells में ज़्यादा portable है, जबकि source ज़्यादा explicit और readable है।
उदाहरण: The . Shorthand
#!/bin/bash
cat <<'EOF' > greetlib.sh
say_hello() {
echo "Hello from the sourced library!"
}
EOF
. ./greetlib.sh
say_hello
Login to try C/C++/Java/PHP code in the editor
Variables भी Share होते हैं
Sourcing सिर्फ functions नहीं बल्कि sourced file में defined plain variables भी share करता है, क्योंकि file का content सीधे current shell के अपने environment में run होता है।
यह sourcing को reusable functions के साथ-साथ configuration values share करने के लिए भी उपयोगी बनाता है।
उदाहरण: Variables Are Shared Too
#!/bin/bash
cat <<'EOF' > config.sh
APP_NAME="MyTool"
VERSION="1.2.0"
EOF
source config.sh
echo "$APP_NAME version $VERSION"
Login to try C/C++/Java/PHP code in the editor
Sourcing Current Shell में Run होता है, एक Subshell में नहीं
bash script.sh या ./script.sh (जो एक बिल्कुल नई child shell start करते हैं जो independently exit होती है) के विपरीत, source script.sh पूरी तरह calling shell के अंदर execute होता है।
यही कारण है कि एक sourced file के अंदर एक exit command पूरी calling script को terminate करता है बजाय सिर्फ इसे control वापस देने के।
exit डालने से बचें -- इसके बजाय return उपयोग करें ताकि यह सिर्फ sourced file के अपने execution को stop करे, पूरी calling shell को नहीं।उदाहरण: Sourcing Runs in the Current Shell, Not a Subshell
#!/bin/bash
cat <<'EOF' > safe_lib.sh
lib_function() {
echo "Library function ran without exiting the caller"
}
EOF
source safe_lib.sh
lib_function
echo "Caller script continues normally after sourcing"
Login to try C/C++/Java/PHP code in the editor
source file.shकोbash file.shया./file.shसे confuse करना; sourcing file के commands को *current* shell में run करती है (इसलिए यह defined functions/variables बाद में available रहते हैं), जबकि normally execute करना इसे एक बिल्कुल नई child shell में run करता है जो script खत्म होने पर अपनी definitions के साथ exit हो जाती है।- यह भूल जाना कि एक sourced file को एक सही relative या absolute path चाहिए; अगर file current directory में नहीं है और
PATHसे नहीं मिलती, तो sourcing 'No such file or directory' के साथ fail होता है। - एक file source करना जो
exitcall करती है; चूंकि sourcing current shell में run होती है, sourced file के अंदर एकexitआपकी *पूरी* current shell/script को terminate करेगा, सिर्फ sourced file से return नहीं।
source filenameऔर. filenameequivalent हैं; दोनों file के contents को current shell में run करते हैं बजाय एक subshell में।- एक sourced file में defined Functions और variables
sourceline complete होने के बाद sourcing script में available रहते हैं। - यह multiple scripts में उपयोग होने वाले functions की एक shared library बनाने का standard तरीका है, जैसे
source ./lib/helpers.sh। - चूंकि sourcing current shell share करती है, sourced file में एक
exitपूरी calling script को खत्म करता है, सिर्फ sourced portion को नहीं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: