The problem is not tee . It is with a pipe. Each command in the pipe is executed in separate subshells.
Thus, any changes made to variables in functions are not reflected.
You can perform the following process substitution, but still it is not equivalent to the pipeline.
FCT_main > >( tee file.out )
Note Process change only works with some shells. This code was found while working with the bash shell.
Whole code
FCT_main() { MY_VAR=2 } MY_VAR=1 echo "MY_VAR=$MY_VAR" # -> print 1. FCT_main > >( tee file.out ) echo "MY_VAR=$MY_VAR" # -> print 1, but I expect 2 ! echo "\n" echo "MY_VAR=$MY_VAR" # -> print 1. FCT_main echo "MY_VAR=$MY_VAR" # -> print 2 as expected.
source share