# Exported variables are suddenly empty

**URL:** <https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** build-env, travis-build, travis-yml\
**Created:** [September 20, 2020, 4:21pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958 "2020-09-20T16:21:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkralphs](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/tkralphs/32/4133_2.png) [@tkralphs](https://travis-ci.community/u/tkralphs)\
**Post date:** [September 20, 2020, 2:24pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958/1 "2020-09-20T14:24:40Z")

</div>

As part of a Travis build, I’m running a relatively simple bash script downloaded from [here](https://github.com/coin-or/coinbrew/blob/master/.ci/setup_environment.sh). The result of running the script on xenial and bionic are inconsistent and in both cases, I’m getting erroneous behavior that I can’t replicate with the same versions of bash running in a Docker container on my local machine. Further, everything works perfectly on MacOS builds with the same script (run with a recent version of bash installed from homebrew).

A recent build that demonstrates the behavior is [here](https://travis-ci.org/github/coin-or/Clp/builds/727571651) and the configuration is [here](https://github.com/coin-or/Clp/blob/ff5cdacd64253d42e4eed6fa824a9ce9a4353a76/.travis.yml). In the first job [here](https://travis-ci.org/github/coin-or/Clp/jobs/727571652) running on bionic, I set the value of the variable `COMMON_ARGS` (a bash array) in the script [here](https://travis-ci.org/github/coin-or/Clp/jobs/727571652#L798) like so:

```auto
COMMON_ARGS=(--no-prompt --verbosity ${VERBOSITY:-2} --tests main --enable-relocatable)

```

Afterwards, I echo it’s value [here](https://travis-ci.org/github/coin-or/Clp/jobs/727571652#L806) (along with the values of some other variables) like so:

```auto
echo "${COMMON_ARGS[@]}" "${ADD_ARGS[@]}" "${DBG_ARGS[@]}"

```

but the variables whose values were just set are now empty.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [September 20, 2020, 4:30pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958/3 "2020-09-20T16:30:47Z")

</div>

**You cannot `export` array variables.** Because an envvar can only be a string and there’s no one true conversion method.

I could not reproduce the issue locally but I can say for sure that `export ARRAY_VAR` does _not_ make `ARRAY_VAR` an envvar:

```auto
$ export COMMON_ARGS=(--no-prompt --verbosity ${VERBOSITY:-2} --tests main --enable-relocatable)
$ printenv | grep COMMON_ARGS
$ 

```

so whether it works or not, it’s pointless anyway.

After removing `export`s from `setup_environment.sh`, `echo` prints values as expected: [https://travis-ci.com/github/native-api/Clp/jobs/387717751#L458-L463](https://travis-ci.com/github/native-api/Clp/jobs/387717751#L458-L463) .

---

<div class="post-metadata">

**Author:** ![tkralphs](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/tkralphs/32/4133_2.png) [@tkralphs](https://travis-ci.community/u/tkralphs)\
**Post date:** [September 20, 2020, 5:20pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958/4 "2020-09-20T17:20:58Z")

</div>

I actually didn’t realize that you couldn’t export arrays, thanks for clarifying that. I actually tried removing all the exports at some point, but that caused other issues, since some of the variables really do need to be exported. Fortunately, the arrays don’t need to be, so I can forgo exporting those as a workaround and continue exporting others.

Nevertheless, this seems to be buggy behavior. Exporting shouldn’t actually _prevent_ the variable from being set locally and it doesn’t do so on most of my builds. The fact that arrays can’t be exported is correct behavior of bash and nothing to do with Travis. The fact that exporting arrays blocks them from being set locally seems to be a Travis bug and is the thing that is relevant here. I will open a new post just focusing on that. It must have to do something with whatever wrapper Travis is using the run these scripts.

Anyway, this has been very helpful in zeroing in on what’s really going on. Thanks!

---

<div class="post-metadata">

**Author:** ![tkralphs](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/tkralphs/32/4133_2.png) [@tkralphs](https://travis-ci.community/u/tkralphs)\
**Post date:** [September 20, 2020, 5:56pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958/5 "2020-09-20T17:56:43Z")

</div>

I marked your first answer as the solution, since the title is now about the `+=` behavior, but the problem description references more than just this issue and I don’t have any way of cleaning that up. Sorry for mixing the two issues, but it wasn’t that clear to me at first that they weren’t related.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [September 21, 2020, 8:15pm UTC](https://travis-ci.community/t/exported-variables-are-suddenly-empty/9958/6 "2020-09-21T20:15:17Z")

</div>

> [@tkralphs](#):
>
> Nevertheless, this seems to be buggy behavior.

Of course. But with reasonable effort, I cannot see what the bug is triggered by. E.g. [if I `echo` a var in the same script, it doesn’t show](https://travis-ci.com/github/native-api/Clp/jobs/388146518#L204), but [if I `echo` it in a separate `.travis.yml` command entry later, it prints correctly](https://travis-ci.com/github/native-api/Clp/jobs/388146518#L285-L286). So this looks like an issue local to a Bash execution context (within a single `eval`). The issue never happens locally, regardless of whether standard streams are a tty or not. Digging any deeper would mean live debugging.
