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=content2-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=content4-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 thisQuestions: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.
What else can break if I remove export from a variable?
shell script;environment variables;variable
You can remove all exports without any effect at all on exported variables so long as you don't use export to twice evaluate. By twice evaluate I mean:var1=var2 export ${var1}=var3echo $var2var3Instead just use:set -a ...at the top of the script. All variables defined thereafter will be automatically exported - which would include variables you might not have previously exported. Alternatively you could only set -a for a portion of the script and later set +a to unset it - it could also work as function.But subshells automatically inherit variable values anyway, so:var1=value( echo $(echo $var1) )valueexport makes no difference in that case.But if your script calls another script, or any other executable that interprets values you've exported and you cease to export them, then those values will no longer be available in their environment. In the following example I use the shell variable $PS1 - which defines the contents of an interactive shell's prompt - to demonstrate how variations on exported variables affect child processes.export PS1=$(printf this is another executable\n > )echo exit | sh -i###OUTPUT###this is another executable > exitexitBut ...PS1=$(printf this is another executable\n > )echo exit | sh -i###OUTPUT###sh-4.3$ exitexitBut then again, if you explicitly declare environment variables while invoking a process...PS1=$(printf this is another executable\n > ){echo exit | PS1=$PS1 sh -iecho exit | sh -i}###OUTPUT###this is another executable > exitexitsh-4.3$ exitexit