Reference से Arrays Pass करने के लिए Nameref Variables (declare -n)
In this page:
declare -n reference_name=variable_name
function_name() {
local -n array_ref=$1
array_ref+=("value")
}
एक Basic Nameref बनाना
declare -n ref=varname ref को varname के लिए एक alias की तरह behave करने देता है: ref पढ़ना या लिखना असल में varname से named variable को indirectly पढ़ता या लिखता है।
उदाहरण: Creating a Basic Nameref
#!/bin/bash
original="hello"
declare -n alias_var=original
echo "through alias: $alias_var"
alias_var="changed"
echo "original is now: $original"
Login to try C/C++/Java/PHP code in the editor
Reference से Caller का Array Modify करना
एक array का नाम एक plain string argument के रूप में pass करना, फिर इसे function के अंदर declare -n से bind करना, function को caller के actual array में append या modify करने देता है, एक disconnected copy पर operate करने के बजाय।
उदाहरण: Modifying a Caller's Array by Reference
#!/bin/bash
add_item() {
local -n target_array=$1
target_array+=("$2")
}
fruits=("apple" "banana")
add_item fruits "cherry"
echo "fruits now: ${fruits[*]}"
Login to try C/C++/Java/PHP code in the editor
Nameref के through Multiple Values Return करना
Namerefs scalar variables के लिए भी काम करते हैं, functions को global state या command substitution पर rely करने के बजाय एक caller-provided variable name में एक computed value "return" करने का एक clean तरीका देते हुए।
square_into result_ref 7 call करना एक self-referencing error बना देगा।उदाहरण: Returning Multiple Values via a Nameref
#!/bin/bash
square_into() {
local -n result_ref=$1
result_ref=$(( $2 * $2 ))
}
square_into answer 7
echo "7 squared is $answer"
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि bash functions normally array arguments अलग words में expand करके receive करते हैं, array structure खोते हुए; array का NAME pass करना और एक nameref उपयोग करना इसे preserve करता है।
- nameref parameter का नाम caller में reference किए जा रहे variable जैसा ही रखना, जो एक self-reference error बना सकता है।
- यह न realize करना कि
declare -nएक bash 4.3+ feature है, इसलिए यह बहुत पुराने bash या POSIX sh के लिए intended scripts में काम नहीं करेगा।
declare -n ref=varnamerefको जो भी variablevarnamenames करता है उसका alias बनाता है, arrays शामिल।- एक array का NAME (न कि
"${arr[@]}") एक argument के रूप में pass करना, फिर इसे function के अंदरdeclare -nसे bind करना, function को caller का actual array पढ़ने और modify करने देता है। - Namerefs function से array data बाहर लाने के लिए global variables उपयोग करने वाले common workaround से बचते हैं।
- nameref parameter को किसी भी variable से distinct नाम दें जिसे यह reference कर सके, "circular reference" error से बचने के लिए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling