It did get set, but only in the shell launched by Ant. Once that shell exited, the value disappeared. I think that even is you ran "sh sample.sh" that foo would not be set in your shell, you would have to do something like ". sample.sh" which causes the script to be run in your current shell.
But rather that trying to do something that is impossible, why don't you tell us why your real requirements are, we might be able to provide some suggestions. Being able to set an env var via Ant is a proposed technical solution to a problem, it is not a requirement. Tell us what the problem is.
I agree with you that the env var foo will not set in the current shell.
But how to set in the current shell? is there any way to do it by ant?
Yes, ". sample.sh" or "source sample.sh" will set the env var foo to bar.
How to do it using ant (build.xml) ?
The requirement is:
If ant runs the target new_target, should set the env var foo to bar.
$ ant new_target