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.

4
  • 1
    That is ugly and unncessary. If you want to pass the array by name, make array_internally an alias of it: declare -n array_internally=$1. And the rest of it about "becomes complicated" and "determine and then remove ..." applies to irrespective of how you pass the array, so I don't see what's the point of that. And evaling an array potentially containing special characters is just waiting for grief to happen at a later date.
    – muru
    Commented Jul 12, 2018 at 4:04
  • Just posted a proposition : pass one or multiple strings and make them an array inside the function.
    – tisc0
    Commented Feb 7, 2020 at 9:28
  • @muru, what do you make of 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 understand how this fits, except to know that absent other input such as your suggestion, the statement would, in my ignorance, discourage me from using the -n option in this situation.
    – ebsf
    Commented Jun 3, 2022 at 18:13
  • @ebsf I believe what it means is that you can't have something like declare -an foo=(...) where a variable is both an array and a nameref. Having an array foo and then doing declare -n bar=foo has worked fine for years, e.g., another answer from 7 years ago on this post, or this answer from me on U&L from 5 years ago
    – muru
    Commented Jun 4, 2022 at 2:38