0

I have two functions and im trying to figure out how to pass a list to a function call from within another function

func1{
files=()

$(func2 ${files[@]})
}

func2{
#do something with the list
}
2
  • 1
    Technically, you cannot pass the array as a single argument; you can only expand the array and pass each element as a separate argument. Commented Jun 29, 2020 at 17:53
  • For example, you couldn't do func2 "${files1[@]}" "${files2[@]}" and have func2 know where the arguments from files1 end and the arguments from files2 begin. Commented Jun 29, 2020 at 17:54

1 Answer 1

2

Make sure you have quotes (") around your list when you do the call to preserve any spaces that might be in the individual strings in the list.

Example:

#!/bin/bash

function func1 {
    files=("foo bar" "hello world")

    func2 "${files[@]}"
}

function func2 {
    for var in "$@"; do
        echo ">$var<"
    done
}

func1

Output:

>foo bar<
>hello world<
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.