How to return an array in bash without using globals?

后端 未结 18 2023
Happy的楠姐
Happy的楠姐 2020-11-29 21:36

I have a function that creates an array and I want to return the array to the caller:

create_array() {
  local my_list=(\"a\", \"b\", \"c\")
  echo \"${my_li         


        
18条回答
  •  一向
    一向 (楼主)
    2020-11-29 22:07

    What's wrong with globals?

    Returning arrays is really not practical. There are lots of pitfalls.

    That said, here's one technique that works if it's OK that the variable have the same name:

    $ f () { local a; a=(abc 'def ghi' jkl); declare -p a; }
    $ g () { local a; eval $(f); declare -p a; }
    $ f; declare -p a; echo; g; declare -p a
    declare -a a='([0]="abc" [1]="def ghi" [2]="jkl")'
    -bash: declare: a: not found
    
    declare -a a='([0]="abc" [1]="def ghi" [2]="jkl")'
    -bash: declare: a: not found
    

    The declare -p commands (except for the one in f() are used to display the state of the array for demonstration purposes. In f() it's used as the mechanism to return the array.

    If you need the array to have a different name, you can do something like this:

    $ g () { local b r; r=$(f); r="declare -a b=${r#*=}"; eval "$r"; declare -p a; declare -p b; }
    $ f; declare -p a; echo; g; declare -p a
    declare -a a='([0]="abc" [1]="def ghi" [2]="jkl")'
    -bash: declare: a: not found
    
    -bash: declare: a: not found
    declare -a b='([0]="abc" [1]="def ghi" [2]="jkl")'
    -bash: declare: a: not found
    

提交回复
热议问题