Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 5
    Though, it wasn't exactly what i want, but it still nice to know how pass by reference work in bash. +1 :) Commented Sep 15, 2015 at 12:27
  • 13
    Requires bash 4.3+
    – dtmland
    Commented May 4, 2019 at 18:47
  • 1
    Bash is not Haskell. You've got to compromise. On a different note, it makes sense to do [ "$1" = arr ] || local -n arr=$1 in place of just local -n arr=$1. That way the argument and the parameter names can coincide. As far as I can tell, the scope in bash is dynamic.
    – x-yuri
    Commented Jan 12, 2021 at 19:13
  • 1
    @Sapphire_Brick why would an array named n be a circular reference? If you changed the name array to n and called the function e.g. copyFiles n then inside the function the local variable arr is created effectively as an alias or synonym of n. local -n arr also means a locally (function) scoped variable so even if you called that n as well it wouldn't be circular. The fact that changing the arr inside the function also changes the value of array outside the function is not circular either, it's because both names ultimately point to values in the same address space.
    – Davos
    Commented Jun 10, 2021 at 13:28
  • 1
    This is quite helpful. I wonder, though, about the statement in man bash-builtins (for the declare -n option, which local incorporates): "The -n attribute cannot be applied to array variables." I don't know enough yet to know what to make of this statement in light of this answer. Can someone comment?
    – ebsf
    Commented Jun 3, 2022 at 18:06