1

I really need to get an array to a subshell so the processes can run together in one script. I know I cannot export an array but is it possible to send it through a pipe to a subshell, and how would I do that? Let's say I have a function running(); what would the content need to be so I could update the array in it when it changes in the main program. I would not like to store the array in files since that would be too slow.

1

Just make a function using myfunction() ( ... ) instead of myfunction() { ... } (so it will run in a subshell) and pass in your array as function arguments.

myfunc() (
  for f in "$@"; do
    printf '%s\n' "$f"
  done
)

myfunc these arguments "are passed" 'to myfunc'

myarray=( "These values" "are set as" part 'of an array' )

myfunc "${myarray[@]}"

Output looks like:

these
arguments
are passed
to myfunc
These values
are set as
part
of an array

Note: That's just an example function, but actually here is a simpler version that does the same thing:

myfunc_simpler() (
  printf '%s\n' "$@"
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.