[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'local -x VARIABLE' does not clear variable for subprocesses
From: |
Arfrever Frehtes Taifersar Arahesis |
Subject: |
Re: 'local -x VARIABLE' does not clear variable for subprocesses |
Date: |
Mon, 5 May 2014 19:13:32 +0200 |
User-agent: |
KMail (GNU/Linux) |
2014-05-05 17:18 Chet Ramey napisaĆ(a):
> On 5/3/14, 7:22 PM, Arfrever Frehtes Taifersar Arahesis wrote:
> > 'local -x VARIABLE' (without assignment of value) does not clear variable
> > for subprocesses.
> > It is regression in bash 4.3.
>
> It's not actually a regression; it's a bug fix. It's worth having the
> discussion again, though.
>
> A variable isn't set until it's assigned a value. A statement like
> `local x' or `export x' doesn't actually create a variable. It creates
> a `placeholder' so that a subsequent assignment will instantiate a
> variable with the right attributes, or change the behavior when a value
> is assigned, but does not assign a value itself. This means that
> something like
>
> export x
> echo ${x-unset}
> printenv x
>
> will display `unset'.
>
> The idea behind the bash-4.3 behavior is that the placeholder local
> variable isn't set, and doesn't really exist. It doesn't shadow a
> global variable with the same name until it gets a value. The bash-4.2
> behavior was inconsistent: variables with attributes but without values
> weren't quite set (value == 0x0, but still visible) and weren't quite
> unset.
Behavior of bash 4.3 is inconsistent between non-subshell subprocesses and
other places:
bash 4.2.47:
$ export VAR{1,2}=abc
$ f() { local VAR1; local -x VAR2; echo "### Normal scope:"; declare -p
VAR{1,2}; echo "### Subshell:"; (declare -p VAR{1,2}); echo "### Non-subshell
subprocess:"; bash -c 'declare -p VAR{1,2}'; }
$ f
### Normal scope:
declare -x VAR1
declare -x VAR2
### Subshell:
declare -x VAR1
declare -x VAR2
### Non-subshell subprocess:
bash: line 0: declare: VAR1: not found
bash: line 0: declare: VAR2: not found
bash 4.3.11:
$ export VAR{1,2}=abc
$ f() { local VAR1; local -x VAR2; echo "### Normal scope:"; declare -p
VAR{1,2}; echo "### Subshell:"; (declare -p VAR{1,2}); echo "### Non-subshell
subprocess:"; bash -c 'declare -p VAR{1,2}'; }
$ f
### Normal scope:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
### Subshell:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
### Non-subshell subprocess:
declare -x VAR1="abc"
declare -x VAR2="abc"
Ability to locally unset a variable also for subprocesses would be preferable
solution.
--
Arfrever Frehtes Taifersar Arahesis
signature.asc
Description: This is a digitally signed message part.