Local Variables और Scope
local वह है जो एक function को अपने working variables private रखने देता है, script में कहीं और उपयोग होने वाले variables को accidentally overwrite किए बिना।In this page:
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
Global Leakage Without local
अगर एक function बिना इसे local declare किए एक variable को assign करता है, वह assignment उसी नाम के global variable को affect करता है (अगर यह मौजूद नहीं था तो इसे बनाते हुए), और change function return होने के बाद भी persist करता है।
यह बड़ी scripts में hard-to-trace bugs का एक आम source है।
local उपयोग करें।उदाहरण: Global Leakage Without local
#!/bin/bash
leaky_function() {
message="set inside function, no local"
}
leaky_function
echo "After call, message is: $message"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
outer() {
local shared_value="from outer"
inner
}
inner() {
echo "inner sees shared_value as: $shared_value"
}
outer
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
check_status() {
local output
output=$(echo "data"; exit 3)
echo "Captured output: $output"
echo "Exit status of the command substitution: $?"
}
check_status
Login to try C/C++/Java/PHP code in the editor
- एक function के working variables के लिए
localउपयोग करना भूल जाना; इसके बिना, script में कहीं और (या किसी और function में) उपयोग होने वाले same नाम वाला एक variable silently overwrite हो जाता है। - यह मान लेना कि
localएक variable को current function के अंदर से call किए functions को visible बनाता है; Bash में local scope सिर्फ उस function में visible है जिसमें यह declared है और जिन functions को यह call करता है (dynamic scoping), callees से hidden नहीं, लेकिन function return होने के बाद caller से hidden है। localको command substitution के साथ एक single line में combine करना, जैसेlocal x=$(cmd), यह realize किए बिना किlocalका अपना exit statuscmdके 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) कोlocaldeclare करना name collisions से होने वाले subtle bugs से बचाता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: