My problem is that when I started coding my script I encountered several variable scope issues which resulted in the bad habit of exporting almost all my variables.
Now that my code has got quite large I was contemplating cleaning it and part of that is removing a bunch of useless exports. Unfortunately I'm not sure if my knowledge of variable scope is complete. Even after reading a few pages on the subject.
What I know (and hopefully is right):
1-Exporting a variable makes it's content available to subshell processes:
export myvar="content"
2-Stuff done in parenthesis like this will be affected by removing exports (from what I understand this is the only way to declare / use a subshell):
$(grep "content" <<< $myvar)
3-Variables declared without setting their scope are global:
myvar="content"
4-Since I don't declare any local variables I don't have to worry about causing issues within my functions:
local myvar="i don't use this"
Questions:
1- Is there any point in proceeding with removing the useless exports beside my code not reeking of obvious noobishness?
2- If I do proceed with it, is there anything else I should be aware of that may be affected and break my code? or is there any of my knowledge wrong/incomplete?
3- If you know of a well written (and complete) variable scope reference please share the link.
export
ed variables is limited to the execution, as opposed to sourcing it, which would drag them into and pollute the current shell environment. – goldilocks Jun 17 '14 at 15:06