Sammy
Sammy

Reputation: 3707

Export array environment variable bash/bitbucket pipelines

I have the following variable in bitbucket deployment settings:

CALLBACKS="https://myapp.com" "http://localhost:3000"

I need to export it to an environment variable of type array that will be read by a script thereafter. I'm trying this but it's not working:

read -a CALLBACKS_ARRAY <<< "${CALLBACKS}"
export CALLBACKS_ARRAY="$(echo ${CALLBACKS_ARRAY})"

Upvotes: 0

Views: 1119

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295815

This cannot be done. Environment variables are NUL-delimited; array definitions separate the items with NULs. Thus, you can only export an environment variable when it's serialized into a string, such that the child process can deserialize it into an array.

What you can do, by contrast, is export BASH_ENV with a filename containing content that, when sourced, will define your array.

Thus:

read -a CALLBACKS_ARRAY <<< "${CALLBACKS}"
BASH_ENV=$(mktemp -t bash_env.XXXXXX)
declare -p CALLBACKS_ARRAY >"$BASH_ENV"
export BASH_ENV

Note that a compliant /bin/sh reads ENV rather than BASH_ENV; that said, since arrays aren't present in the baseline POSIX sh standard, there's not much need to worry about that here.

Upvotes: 3

Related Questions