I've found nothing about this in all my searching.
In the code below, Bar1
acts like I'd expect a "regular" variable to act (since help declare
says that -g
is only "seen" in function calls): it changes value depending on the scope (outer or inner).
Bar2
, being "plainly" declared, also acts like I expect (being gfhjnfd
in the outer scope, and blat
in the inner scope.
But Foo
is a globally unchanging variable. help declare
only says "to make NAMEs readonly". Where is this extended (and unwanted) feature documented?
$ cat test.sh
#!/bin/bash
declare -r Foo=bar
declare -g Bar1=fdhtbn
declare Bar2=gfhjnfd
X()
{
local Foo=$1
local Bar1=snorkel
local Bar2=$2
echo Foo in X = $Foo
echo Bar1 in X = $Bar1
echo Bar2 in X = $Bar2
}
echo Foo, above X, = $Foo
echo Bar1, above X = $Bar1
echo Bar2, above X = $Bar2
echo
X baz blat
echo
echo Foo, below X, = $Foo
echo Bar1, below X = $Bar1
echo Bar2, below X = $Bar2
$ ./test.sh
Foo, above X, = bar
Bar1, above X = fdhtbn
Bar2, above X = gfhjnfd
./test.sh: line 9: local: Foo: readonly variable
Foo in X = bar
Bar1 in X = snorkel
Bar2 in X = blat
Foo, below X, = bar
Bar1, below X = fdhtbn
Bar2, below X = gfhjnfd
local Foo
to remove the read-only-ness from the outerFoo
variable? You can't do that.declare -g
at the global scope doesn't do anything interesting. The-g
option is for declaring a named inside a function as global, asdeclare foo
inside a function implicitly makes the variable local, just as if you had usedlocal
.