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.

Required fields*

7
  • Thanks. (1) "you can't call this function with an array called arr due to the particular name scoping rules used by bash." Do you mean arr declared as typeset -n arr=$1 inside the function body has global (script-wide) scope? (2) Among the three approaches when creating a bash function, which one do you suggest more or none? Commented Nov 17, 2018 at 21:08
  • @Tim (1) A name reference variable can't reference another variable that has the same name as itself (see also Circular name references in bash shell function, but not in ksh). (2) It depends on what you'd like to do and how you want/need to get there. If the original data was coming from a file, and wasn't used for anything else, I would probably go with reading the array from sed instead of reading it and manipulating it. If the resulting data would go to a file, I would not read it into an array at all. Commented Nov 17, 2018 at 21:13
  • Thanks. "If the resulting data would go to a file, I would not read it into an array at all." Could you be more specific? Commented Nov 17, 2018 at 22:39
  • @Tim Well, if all you want to do is to duplicate the internal lines of a text file and write that to a new file, then there is that sed command that could do that for you. Why read it into an array in bash? Commented Nov 17, 2018 at 22:41
  • 1
    @Cbhihe No, you are misreading. It says "Array variables cannot be given the nameref attribute. However, nameref variables can reference array variables and subscripted array variables." In my code, arr (in the function) is not an array variable, it's an ordinary variable that I give the nameref attribute to. It then references an array variable. Commented Nov 18, 2018 at 17:55