0

I have 2 files, to perform some deployment operation.

helper.sh

 #!/usr/bin/env bash

// some code

      
function2() {
      local initial_asg_list=(${1})
        local region=${2}
        local current_build=${3}
        local modified_shard_list=()
        .
        //some processing to populate modified_shard_list
        echo  "${modified_shard_list[*]}"
    }

and another file is

File2.sh

#!/usr/bin/env bash
    . "./helper.sh"
     
    //some code
    //some more code
    
    
     function1() {
        local shard_list=(shard1_name shard2_name shard3_name shard4_name)
        local shard_list_to_process=($(function2 ${shard_list[*]}", "param2" "param3"))
         echo "value obtained is ${shard_list_to_process[*]}"
        }

on execution of function2 , output is

value obtained is 1

I refereed many docs, some say to use declare etc , but I dont want to use declare. Where exactly I am missing.If I want to use same code. Also , I need to maintain these 2 functions in separate file. Note, I tries with echo "${modified_shard_list[@]}" in helper file, getting same issue.

KCS
  • 2,937
  • 4
  • 22
  • 32
  • The whole concept of "returning an array" makes no sense. – Charles Duffy Jun 07 '21 at 16:28
  • And arrays don't _exist_ in `sh`, so this question certainly shouldn't be tagged `sh`. If you're using bash arrays, tag bash; if you're using ksh arrays, tag ksh; etc. – Charles Duffy Jun 07 '21 at 16:28
  • `${array[*]}` **generates a single string**; you no longer have an array when you do that. – Charles Duffy Jun 07 '21 at 16:29
  • I need value of modified_shard_list for further processing in function1, then how to obtain – KCS Jun 07 '21 at 16:29
  • `printf '%s\0' "${array[@]}"` emits your array as a stream of NUL-delimited strings; with bash 5, `readarray -d '' new_array < <(...thing that emits that stream...)` loads the stream into a calling array. Or you can use globals, or you can use named return values with namevars. – Charles Duffy Jun 07 '21 at 16:33
  • But really, it's often better to just go the namevar approach, assuming you're targeting a new enough version of bash to support it (IIRC, it's a 4.3 feature). – Charles Duffy Jun 07 '21 at 16:34
  • No, `echo "${array[@]}"` doesn't help at all, it still forms a string. What part of my comment made you think it would be any different? `echo "${array[@]}"` passes each element as a separate argument to `echo` (preserving array-ness that one extra step), but then `echo` combines those arguments together into a single string, so even though it happens at a slightly different place, you're still destroying your array and creating a string to replace it in the end. – Charles Duffy Jun 07 '21 at 16:34
  • 1
    ...that's why I specifically instructed `printf '%s\0' "${array[@]}"`, and then using code capable of handling NUL delimiters on the read side. (If you're in a version of bash too old to have `readarray -d ''`, you can use `array=(); while IFS= read -r -d '' item; do array+=( "$item" ); done < <(...thing that generates a NUL-delimited stream...)` as an alternative. – Charles Duffy Jun 07 '21 at 16:36

0 Answers0