Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script-local variables not correctly substituted when functions are called #5054

Closed
mqudsi opened this issue Jun 18, 2018 · 3 comments
Closed
Labels

Comments

@mqudsi
Copy link
Contributor

mqudsi commented Jun 18, 2018

I'll preface this right off the bat with "I don't know if this is by design, but I sure am confused."

set -l foo

function print_foo
	echo "Evaluating print_foo"
	echo "inner foo: $foo"
end

set foo "foo"
echo "setting foo to \"$foo\""
print_foo

This prints the following:

setting foo to "foo"
Evaluating print_foo
inner foo: 

As I understand it, the first line of the script defines a variable local to the script named foo. While a function block defines a scope, the declaration of a new scope does not wipe out variables defined in the parent scope.

I can understand if the variable foo is not defined if I were to call print_foo from outside this script (after sourcing it), though I would consider that a bug since a "reference" to that variable still exists and therefore it shouldn't be garbage cleaned. But I don't understand why when print_foo is called from within the same script file that print_foo is defined in, the value is not retained.

Additionally, I am vexed because the behavior below differs:

set -l foo "foo"
for i in ""
    echo "Inner foo: \"$foo\""
end

prints Inner foo: "foo", even though the for block also defines a new scope just like the function block does.

@krader1961
Copy link
Contributor

See flag --no-scope-shadowing in man function. This is by design. The fact you defined foo with local rather than global scope means it is equivalent to this:

function bar
    set -l foo baz
    print_foo
end

Whether the design is sensible is a different question 😄

@faho
Copy link
Member

faho commented Jun 18, 2018

Yes, this is by design.

@mqudsi: If you want the function to inherit a copy of the variable, use function --inherit-variable or export the variable. If you want it to be able to modify the variable, you should make it global instead.

@faho faho added the question label Jun 18, 2018
@mqudsi
Copy link
Contributor Author

mqudsi commented Jun 19, 2018

Awesome, thanks guys.

@mqudsi mqudsi closed this as completed Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants