typeset and CentOS 7 behavior

100 views Asked by At

Clear CentOS 7.9, the simple bash script

function unexpected_behavior() {
  local action="install"
  typeset -g action="$action"
}

unexpected_behavior
typeset

If a local variable and typeset global variable names the same, after executing unexpected_behavior function no action variable in typeset.

[centos@ip-10-20-1-44 ~]$ ./test.sh
…
XDG_SESSION_ID=4
_=unexpected_behavior
unexpected_behavior ()
{
    local action="install";
    typeset -g action="$action"
}

CentOS 8 and Ubuntu have different behavior, action variable exists in typeset.

ubuntu@ip-10-20-1-166:~$ ./test.sh
…
XDG_SESSION_ID=1
XDG_SESSION_TYPE=tty
_=unexpected_behavior
action=install
unexpected_behavior ()
{
    local action="install";
    typeset -g action="$action"
}

Can you explain to me the reason for this strange behavior?

1

There are 1 answers

0
pepoluan On

It was a bug fixed in bash 4.3

ddd. Fixed several bugs that caused `declare -g' to not set the right global variables or to misbehave when declaring global indexed arrays.

(declare and typeset are exact synonyms)