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

Local Variables और Scope

By default Bash में हर variable global होता है, तो local वह है जो एक function को अपने working variables private रखने देता है, script में कहीं और उपयोग होने वाले variables को accidentally overwrite किए बिना।
Syntax
bash
function_name() {
  local variable_name=value
  commands
}

Why local Matters

local के बिना, एक function के अंदर assign किया कोई भी variable एक global variable बन जाता है (या इसे overwrite करता है), जो script में कहीं और state को silently corrupt कर सकता है जो उसी नाम का उपयोग करता हो।

local varname=value variable को strictly उस function call तक confine करता है।

उदाहरण: Why local Matters

bash
#!/bin/bash
counter=100
increment() {
    local counter=0
    counter=$((counter + 1))
    echo "Inside function, counter is $counter"
}
increment
echo "Outside function, counter is still $counter"

Global Leakage Without local

अगर एक function बिना इसे local declare किए एक variable को assign करता है, वह assignment उसी नाम के global variable को affect करता है (अगर यह मौजूद नहीं था तो इसे बनाते हुए), और change function return होने के बाद भी persist करता है।

यह बड़ी scripts में hard-to-trace bugs का एक आम source है।

Warning: अपने global को specifically modify करने का इरादा न हो तो function के अपने working variables के लिए हमेशा local उपयोग करें।

उदाहरण: Global Leakage Without local

bash
#!/bin/bash
leaky_function() {
    message="set inside function, no local"
}
leaky_function
echo "After call, message is: $message"

Dynamic Scoping: Callees See Caller's Locals

Bash के locals dynamically scoped हैं, मतलब दूसरे function के अंदर से call किया गया एक function उस outer function के local variables देख और उपयोग कर सकता है, lexically scoped languages के विपरीत जहां सिर्फ enclosing code block मायने रखता है।

यह एक distinctive quirk है जानने लायक भले ही ज़्यादातर scripts इस पर directly rely न करें।

उदाहरण: Dynamic Scoping: Callees See Caller's Locals

bash
#!/bin/bash
outer() {
    local shared_value="from outer"
    inner
}
inner() {
    echo "inner sees shared_value as: $shared_value"
}
outer

Separating local Declaration from Command Substitution

local को एक command substitution assignment के साथ एक line में combine करना, जैसे local x=$(cmd), cmd के अपने exit status को local के exit status के पीछे hide कर सकता है।

Declaration और assignment को दो lines में split करना substituted command के real exit status check करने की ability preserve करता है।

उदाहरण: Separating local Declaration from Command Substitution

bash
#!/bin/bash
check_status() {
    local output
    output=$(echo "data"; exit 3)
    echo "Captured output: $output"
    echo "Exit status of the command substitution: $?"
}
check_status
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. एक function के working variables के लिए local उपयोग करना भूल जाना; इसके बिना, script में कहीं और (या किसी और function में) उपयोग होने वाले same नाम वाला एक variable silently overwrite हो जाता है।
  2. यह मान लेना कि local एक variable को current function के अंदर से call किए functions को visible बनाता है; Bash में local scope सिर्फ उस function में visible है जिसमें यह declared है और जिन functions को यह call करता है (dynamic scoping), callees से hidden नहीं, लेकिन function return होने के बाद caller से hidden है।
  3. local को command substitution के साथ एक single line में combine करना, जैसे local x=$(cmd), यह realize किए बिना कि local का अपना exit status cmd के exit status को mask कर सकता है -- अगर आपको cmd का status चाहिए, तो दो अलग lines में declare और assign करें।
चैप्टर सारांश
  • एक function के अंदर local varname=value एक variable बनाता है जो सिर्फ उस function call के अंदर मौजूद होता है और function return होने पर गायब हो जाता है।
  • local के बिना, एक function के अंदर assign किया variable by default global होता है और function return होने के बाद persist/overwrite करता है।
  • Bash locals के लिए dynamic scoping उपयोग करता है: दूसरे function के अंदर से call किया गया एक function caller के locals *देख सकता है*, कई दूसरी languages में lexical scoping के विपरीत।
  • Functions के अंदर आम नाम वाले loop या temp variables (i, result, temp) को local declare करना name collisions से होने वाले subtle bugs से बचाता है।

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.