From 163da33957c6b9405a4c4d2779c6669bf73383d5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Sat, 17 Sep 2022 15:42:53 +0200 Subject: [PATCH 1/2] Remove `declare` from array definitions. `declare -a` is used for arrays when `declare -p` dumps the configuration. However when `pgenv_configuration_read` gets back the configuration, variables becomes locally scoped (see `bash -c "help declare"). One solution could be to use `declare -g` to make variables globals, but this does not works on OSX. Removing `declare -a` from arrays seems to make the variable global even if no `EXPORT` is issued. As a possible compatibility statement, the `EXPORT` after each variable is left in place. Close #56 --- bin/pgenv | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/pgenv b/bin/pgenv index 693ab59..71308b2 100755 --- a/bin/pgenv +++ b/bin/pgenv @@ -2,7 +2,7 @@ # # VERSION # -PGENV_VERSION="1.3.1" +PGENV_VERSION="1.3.2" # https://stackoverflow.com/a/19622569/79202 trap 'exit' ERR @@ -620,9 +620,10 @@ pgenv_configuration_write_variable(){ if [[ "$name" =~ _OPTIONS$ ]]; then - # declare has no way to output a global variable - # that is then needed when reloading configuration ! - declare -p "${name}" >> "$file" + # using `declare` will make the variable `local` + # once it is read from the pgenv_configuration_read function + # so don't use it and export as a global variable + declare -p "${name}" | sed 's/^declare -a\ //' >> "$file" echo "export ${name}" >> "$file" else # if no value supplied, put a comment From 156c11cf11ca70afdb2a31bd12d2fce5db0ec51d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Sep 2022 08:51:15 +0200 Subject: [PATCH 2/2] Remove the `x` flag appended to `declare`. When the configuration is dumped, the `declare -p` produces also the `-x` flag due to the presence of the explicit `EXPORT`. --- bin/pgenv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pgenv b/bin/pgenv index 71308b2..8ba88a5 100755 --- a/bin/pgenv +++ b/bin/pgenv @@ -623,7 +623,7 @@ pgenv_configuration_write_variable(){ # using `declare` will make the variable `local` # once it is read from the pgenv_configuration_read function # so don't use it and export as a global variable - declare -p "${name}" | sed 's/^declare -a\ //' >> "$file" + declare -p "${name}" | sed 's/^declare -ax\? //' >> "$file" echo "export ${name}" >> "$file" else # if no value supplied, put a comment