On Tue, Nov 19, 2024 at 01:40:12AM -0500, Harry Clauson wrote: > URL: > <https://savannah.gnu.org/bugs/?66460> > > Summary: A documentation correction regarding an array > nameref??? > Group: The GNU Bourne-Again SHell > Submitter: harryc > Submitted: Tue 19 Nov 2024 06:40:06 AM UTC > Category: None > Severity: 3 - Normal > Item Group: None > Status: None > Privacy: Public > Assigned to: None > Open/Closed: Open > Discussion Lock: Any > > > _______________________________________________________ > > Follow-up Comments: > > > ------------------------------------------------------- > Date: Tue 19 Nov 2024 06:40:06 AM UTC By: Harry Clauson <harryc> > I was surprised that the documentation for "declare -n" states: > > "The nameref attribute cannot be applied to array variables." > > This appears to be incorrect as I frequently pass arrays using a nameref to > avoid the overhead of making a copy of large amounts of data. > > For example: > > #################################################### > function doit { > local -n blahref=$1 > echo ${blahref[2]} # displays z > blahref[2]=a > blahref[3]=b > return 0 > } > > declare -a blah > blah=(x y z) > > echo ${blah[2]} # displays z > > doit blah > echo ${blah[2]} # displays a > echo ${blah[3]} # displays b > #################################################### >
The code above does not apply the nameref attribute to an array. Applying the nameref attribute to an array would be doing this: $ declare -n ref[0]=a bash: declare: ref[0]: reference variable cannot be an array > In addition, when browsing the internet I see many questions regarding how to > pass an array by reference, and the answers do not mention simply using a > nameref. And if not using a nameref it appears that they are actually passing > data by value which is inefficient, especially in the case of arrays. > > This leads me to believe that users who have read the documentation are not > using this valuable feature, nor are they passing it along to others when > answering related questions. > > Please let me know if I am missing something here or if the documentation > should be corrected in this regard. > > Thank you! > > > > > > > > > > _______________________________________________________ > > Reply to this item at: > > <https://savannah.gnu.org/bugs/?66460> > > _______________________________________________ > Message sent via Savannah > https://savannah.gnu.org/ -- Andreas (Kusalananda) Kähäri Uppsala, Sweden .