That's what happens when a command is executed in a subshell environment:
The command will run in a copy of the current shell execution environment
"Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes" (quote)
Example:
#!/bin/sh
export TOTO=123
FOO=abc
(mycmd)
In this case mycmd will be able to read TOTO but not FOO, and every changement realized by mycmd to the values of those two variables will not be visible in the current shell.
But what happens when we do the same thing with a function?
Example:
#!/bin/sh
export TOTO=123
FOO=abc
function (){
echo $TOTO
echo $FOO
TOTO=${TOTO}456
FOO=${FOO}def
}
(function)
echo $TOTO
echo $FOO
result:
123
abc
123
abc
Reasonably a function executed in a subshell is not able to alter the contents of the variables defined in the parent shell, on the other hand it is able to read all the variables indiscriminately even if they are not being exported.
Can somebody please explain this behaviour in a better way? Links to some reference will be very appreciated since I couldn't find anything about it.