[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: declare -g var should return a nonzero return code when it fails

From: Peggy Russell
Subject: Re: declare -g var should return a nonzero return code when it fails
Date: Mon, 4 Nov 2013 09:24:12 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

> > "declare -g var" should return a nonzero return code when it fails.
> There is a problem here, and the problem that causes the spurious error
> message from test_error has been fixed in bash-4.3. However, I don't think
> you completely understand what bash scoping is, or what makes a variable
> set or unset.

Is the problem that is fixed in 4.3, that "declare -p" returns "bash:
declare: var: not found" for variables that are unset?  

I find "declare -p" very helpful to see variables from bash's perspective.
I combined the bash man page sections PARAMETERS (name=[value]) and declare,
along with the output of "declare -p" and for the longest time thought only
"declare var=" (default null or 0 using -i) and "declare var=value" were the
only correct syntax. Things make a lot more sense now.

> First, using declare without assigning a value creates a variable
> "placeholder".  The variable exists as more-or-less invisible, but is not
> considered set until it is assigned a value.  Therefore, declare -g var
> just creates a placeholder at the global scope if there is no existing
> global variable with that name.  If there is such a variable, it's a
> no-op.  The `redeclare local variable as global' comment is just > incorrect.

Poor choice of words. Just experimenting here to understand the bounds. A
local variable is "unset", and a new globally scoped variable with the same
name is created. But a globally scoped variable is not created... 
The "placeholder" is still there for the locally scoped variable, isn't it,
and this goes back to the beginning of this thread and the fix for 4.3...
"declare -p" gives the impression that bash knows nothing about the locally
scoped variable.

function declare_local_unset_global_assign() {
  local var='local'
  unset var
  # declare -p var
  declare -g var=${FUNCNAME}

This makes a lot more sense. As far as return codes go it is 0 because it
isn't failing at all. Declaring a global variable that already exists as a
globally scoped variable is a no-op, and in a function declaring a global
variable that already exists as a locally scoped variable, bash uses the
locally scoped variable. 

Thank you.
Peggy Russell

reply via email to

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