4

I want to return an array from the function on bash and use this array in another function. But I get a string, not an array, can you please help me about how can I return an array from bash function, I am new in bash scripting, thanks.

array(){
          local words=("a a" "b b" "c c")
           echo ${words[@]}
    }

    getWord(){
           words=$(array)
           for word in "${words[@]}"; do
                echo "$word"
            done
    }

    getWord

It returns string of a a b b c c but my expected result should be an array.

Jan
  • 125
  • 1
  • 9

1 Answers1

6

Actually, looking at your code, you don't need to return anything; the "words" variable is global and thus can be used in the whole script.

WORKAROUNDS:

EDIT:

#!/bin/bash

array(){
       local words=("a" "b" "c")
       echo "${words[@]}"
}

getWord(){
       local arr=( $(array) )
       for word in "${arr[@]}"; do
            echo "$word"
        done
}

getWord

EDIT2:

#!/bin/bash

orig_IFS="$IFS"
array_IFS="," #Or whatever you want, mb a safer one

array(){
       IFS="${array_IFS}"
       local words=("a a" "b b" "c c")
       echo "${words[*]}"
       IFS="${orig_IFS}"
}

getWord(){
       IFS="${array_IFS}"
       arr=( $(array) )
       IFS="${orig_IFS}"
       for word in "${arr[@]}"; do
            echo "$word"
        done
}

getWord

EDIT3: as suggested per @Kamil Cuk

#!/bin/bash

array_IFS=$'\ca' #Maybe this is safer than using a single comma

array(){
       IFS="${array_IFS}" local words=("a a" "b b" "c c")
       echo "${words[*]}"
}

getWord(){
       IFS="${array_IFS}" arr=( $(array) )
       for word in ${arr[@]}; do #we don't need double quotes anymore
            echo "$word"
        done
}

getWord

Note the slight differences.

danrodlor
  • 1,329
  • 8
  • 16
  • sorry I have revised the code the variable of words is local – Jan Jun 05 '19 at 08:36
  • Is there any good reason for that being that way? – danrodlor Jun 05 '19 at 08:38
  • actually, this code is just an example the main problem is how can I return an array from one function and use it in another function. – Jan Jun 05 '19 at 08:40
  • Using global variables. You can workaround the problem, though. See my edit. – danrodlor Jun 05 '19 at 08:47
  • your code is not working properly when we have an array of words=("a a" "b b" "c c"), see the edit – Jan Jun 05 '19 at 08:57
  • That's why it is a workaround. Furthermore, that was not your original code/question. However, it can be fixed by doing some simple local IFS tweaks. – danrodlor Jun 05 '19 at 09:00
  • @Jan, check out the 2nd edit. – danrodlor Jun 05 '19 at 10:17
  • thanks, it works as expected, but why here used arr=( $(array) ) and what is different with arr=$(array) – Jan Jun 05 '19 at 10:48
  • arr=( $(array) ) transforms the output of the subshell (string) into a list (array). On the other hand, arr=$(array) just sets the resulting string value to the variable arr. – danrodlor Jun 05 '19 at 10:53
  • 2
    The `IFS="${array_IFS}" arr=( $(array) )` is written as a single line `IFS="$array_IFS" arr=( $(array) )`. That way you don't need to save IFS. – KamilCuk Jun 05 '19 at 11:09
  • You're right, that's a cleaner approach! – danrodlor Jun 05 '19 at 11:21