← Back to Bash Course | Chapter 13: Advanced Scripting | Lesson 5 of 7

Reference से Arrays Pass करने के लिए Nameref Variables (declare -n)

एक nameref एक variable है जो किसी दूसरे variable के nickname जैसा act करता है, एक function को caller के array तक पहुंचने और modify करने देते हुए।
Syntax
bash
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

bash
#!/bin/bash
original="hello"
declare -n alias_var=original
echo "through alias: $alias_var"
alias_var="changed"
echo "original is now: $original"

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

bash
#!/bin/bash
add_item() {
    local -n target_array=$1
    target_array+=("$2")
}

fruits=("apple" "banana")
add_item fruits "cherry"
echo "fruits now: ${fruits[*]}"

Nameref के through Multiple Values Return करना

Namerefs scalar variables के लिए भी काम करते हैं, functions को global state या command substitution पर rely करने के बजाय एक caller-provided variable name में एक computed value "return" करने का एक clean तरीका देते हुए।

Warning: nameref parameter का नाम वही रखने से बचें जो pass किया गया variable name है, उदाहरण के लिए square_into result_ref 7 call करना एक self-referencing error बना देगा।

उदाहरण: Returning Multiple Values via a Nameref

bash
#!/bin/bash
square_into() {
    local -n result_ref=$1
    result_ref=$(( $2 * $2 ))
}

square_into answer 7
echo "7 squared is $answer"
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. यह भूल जाना कि bash functions normally array arguments अलग words में expand करके receive करते हैं, array structure खोते हुए; array का NAME pass करना और एक nameref उपयोग करना इसे preserve करता है।
  2. nameref parameter का नाम caller में reference किए जा रहे variable जैसा ही रखना, जो एक self-reference error बना सकता है।
  3. यह न realize करना कि declare -n एक bash 4.3+ feature है, इसलिए यह बहुत पुराने bash या POSIX sh के लिए intended scripts में काम नहीं करेगा।
चैप्टर सारांश
  • declare -n ref=varname ref को जो भी variable varname names करता है उसका 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 से बचने के लिए।

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.