Skip to content

Commit

Permalink
Use global variable definition into configuration.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fluca1978 committed May 5, 2022
1 parent e011310 commit a547e3e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions bin/pgenv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# VERSION
#
PGENV_VERSION="1.2.1"
PGENV_VERSION="1.2.2"

# https://stackoverflow.com/a/19622569/79202
trap 'exit' ERR
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a547e3e

Please sign in to comment.