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

Functions Share करने के लिए Files Source करना

source command (या इसका shorthand .) एक दूसरी script के commands को सीधे आपके current shell के अंदर run करता है, जो multiple script files के बीच functions और variables share करने का तरीका है।
Syntax
bash
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

bash
#!/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)"

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

bash
#!/bin/bash
cat <<'EOF' > greetlib.sh
say_hello() {
    echo "Hello from the sourced library!"
}
EOF
. ./greetlib.sh
say_hello

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

bash
#!/bin/bash
cat <<'EOF' > config.sh
APP_NAME="MyTool"
VERSION="1.2.0"
EOF
source config.sh
echo "$APP_NAME version $VERSION"

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 वापस देने के।

Warning: Source किए जाने वाली file में एक bare exit डालने से बचें -- इसके बजाय return उपयोग करें ताकि यह सिर्फ sourced file के अपने execution को stop करे, पूरी calling shell को नहीं।

उदाहरण: Sourcing Runs in the Current Shell, Not a Subshell

bash
#!/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"
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. 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 हो जाती है।
  2. यह भूल जाना कि एक sourced file को एक सही relative या absolute path चाहिए; अगर file current directory में नहीं है और PATH से नहीं मिलती, तो sourcing 'No such file or directory' के साथ fail होता है।
  3. एक file source करना जो exit call करती है; चूंकि sourcing current shell में run होती है, sourced file के अंदर एक exit आपकी *पूरी* current shell/script को terminate करेगा, सिर्फ sourced file से return नहीं।
चैप्टर सारांश
  • source filename और . filename equivalent हैं; दोनों file के contents को current shell में run करते हैं बजाय एक subshell में।
  • एक sourced file में defined Functions और variables source line 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 को नहीं।

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.