bug-bash
[Top][All Lists]
Advanced

[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 23:27:05 +0200
User-agent: KMail (GNU/Linux)

2014-05-05 22:12 Chet Ramey napisaƂ(a):
> > Ability to locally unset a variable also for subprocesses would be 
> > preferable solution.
> 
> OK.  I'm not sure exactly what this means.  Can you give me an example?
> How would you use this in a situation where `unset' or `declare +x' are
> unusable?

I would like to be able to unexport a variable for subprocesses called in given 
function, but
not subprocesses called later outside of given function.
Example of intended behavior (which happens to be behavior of bash 4.2):

$ export VAR=abc
$ f() { local -x VAR; bash -c 'declare -p VAR'; }
$ f
bash: line 0: declare: VAR: not found
$ bash -c 'declare -p VAR'
declare -x VAR="abc"

Bare 'unset' also unexports a variable for subprocesses called later outside of 
given function
(both bash 4.2 and 4.3):

$ export VAR=abc
$ f() { unset VAR; bash -c 'declare -p VAR'; }
$ f
bash: line 0: declare: VAR: not found
$ bash -c 'declare -p VAR'
bash: line 0: declare: VAR: not found


'local' + 'unset' have effect on normal scope and subshells, but not on 
non-subshell subprocesses
(both bash 4.2 and 4.3):

bash 4.2:

$ export VAR{1,2,3,4}=aaa
$ f() { local VAR1 VAR2=bbb; local -x VAR3 VAR4=bbb; echo "###### Before 
unsetting:"; echo "### Normal scope:"; declare -p VAR{1,2,3,4}; echo "### 
Subshell:"; (declare -p VAR{1,2,3,4}); echo "### Non-subshell subprocess:"; 
bash -c 'declare -p VAR{1,2,3,4}'; unset VAR{1,2,3,4}; echo "###### After 
unsetting:"; echo "### Normal scope:"; declare -p VAR{1,2,3,4}; echo "### 
Subshell:"; (declare -p VAR{1,2,3,4}); echo "### Non-subshell subprocess:"; 
bash -c 'declare -p VAR{1,2,3,4}'; }
$ f
###### Before unsetting:
### Normal scope:
declare -x VAR1
declare -x VAR2="bbb"
declare -x VAR3
declare -x VAR4="bbb"
### Subshell:
declare -x VAR1
declare -x VAR2="bbb"
declare -x VAR3
declare -x VAR4="bbb"
### Non-subshell subprocess:
bash: line 0: declare: VAR1: not found
declare -x VAR2="bbb"
bash: line 0: declare: VAR3: not found
declare -x VAR4="bbb"
###### After unsetting:
### Normal scope:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
bash: declare: VAR3: not found
bash: declare: VAR4: not found
### Subshell:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
bash: declare: VAR3: not found
bash: declare: VAR4: not found
### Non-subshell subprocess:
declare -x VAR1="aaa"
declare -x VAR2="aaa"
declare -x VAR3="aaa"
declare -x VAR4="aaa"
$ bash -c 'declare -p VAR{1,2,3,4}'
declare -x VAR1="aaa"
declare -x VAR2="aaa"
declare -x VAR3="aaa"
declare -x VAR4="aaa"

bash 4.3:

$ export VAR{1,2,3,4}=aaa
$ f() { local VAR1 VAR2=bbb; local -x VAR3 VAR4=bbb; echo "###### Before 
unsetting:"; echo "### Normal scope:"; declare -p VAR{1,2,3,4}; echo "### 
Subshell:"; (declare -p VAR{1,2,3,4}); echo "### Non-subshell subprocess:"; 
bash -c 'declare -p VAR{1,2,3,4}'; unset VAR{1,2,3,4}; echo "###### After 
unsetting:"; echo "### Normal scope:"; declare -p VAR{1,2,3,4}; echo "### 
Subshell:"; (declare -p VAR{1,2,3,4}); echo "### Non-subshell subprocess:"; 
bash -c 'declare -p VAR{1,2,3,4}'; }
$ f
###### Before unsetting:
### Normal scope:
bash: declare: VAR1: not found
declare -x VAR2="bbb"
bash: declare: VAR3: not found
declare -x VAR4="bbb"
### Subshell:
bash: declare: VAR1: not found
declare -x VAR2="bbb"
bash: declare: VAR3: not found
declare -x VAR4="bbb"
### Non-subshell subprocess:
declare -x VAR1="aaa"
declare -x VAR2="bbb"
declare -x VAR3="aaa"
declare -x VAR4="bbb"
###### After unsetting:
### Normal scope:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
bash: declare: VAR3: not found
bash: declare: VAR4: not found
### Subshell:
bash: declare: VAR1: not found
bash: declare: VAR2: not found
bash: declare: VAR3: not found
bash: declare: VAR4: not found
### Non-subshell subprocess:
declare -x VAR1="aaa"
declare -x VAR2="aaa"
declare -x VAR3="aaa"
declare -x VAR4="aaa"
$ bash -c 'declare -p VAR{1,2,3,4}'
declare -x VAR1="aaa"
declare -x VAR2="aaa"
declare -x VAR3="aaa"
declare -x VAR4="aaa"


'declare +x' (or 'local +x') fails to unexport a variable when called in a 
function (both bash 4.2 and 4.3):

$ export VAR=abc
$ f() { declare +x VAR; bash -c 'declare -p VAR'; }
$ f
declare -x VAR="abc"
$ declare +x VAR
$ bash -c 'declare -p VAR'
bash: line 0: declare: VAR: not found

--
Arfrever Frehtes Taifersar Arahesis

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]