From a547e3eaa4d21da5838fc6aeb0326f2c66a7b604 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Thu, 5 May 2022 08:32:37 +0200 Subject: [PATCH] Use global variable definition into configuration. As reported in issue #52, the automatically generated configuration exploits Bash `declare` to define variable. When `pgenv_configuration_load` does `source` the configuration file, declared variables are locally scoped as reported in the `declare` help: When used in a function, `declare' makes NAMEs local, as with the `local' command. The `-g' option suppresses this behavior. Therefore every `declare` statement has to be prefix with `-g`. Unluckily it seems that the counterpart `declare`, used to print out the variable in `pgenv_configuration_write` does not outputs the `-g` flag, probably because at that time the variable is already global. Therefore, before hitting the configuration file, I append the `-g` option to `declare` with a regexp. This `-g` works on Bash 4.4 and 5 on my Linux machines. Bumped version number. Close #52 --- bin/pgenv | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/pgenv b/bin/pgenv index b4491c7..9f6f778 100755 --- a/bin/pgenv +++ b/bin/pgenv @@ -2,7 +2,7 @@ # # VERSION # -PGENV_VERSION="1.2.1" +PGENV_VERSION="1.2.2" # https://stackoverflow.com/a/19622569/79202 trap 'exit' ERR @@ -619,7 +619,9 @@ pgenv_configuration_write_variable(){ # if no value supplied, put a comment if [[ "$name" =~ _OPTIONS$ ]]; then - typeset -p "${name}" >> "$file" + # declare has no way to output a global variable + # that is then needed when reloading configuration ! + declare -p "${name}" | sed 's/declare/declare -g /' >> "$file" else if [ -z "$value" ]; then echo -n "#" >> "$file"