Sometimes we may want to preserve the environment variables of the original user when using sudo.
If you want to preserve all env variables, you could use the -E option. But use it with caution.
sudo -E bash -c 'echo $PATH'
A better approach is to list those env variables which u want to preserve in the sudoers config file. For example, i want to keep the HTTP_PROXY and HTTPS_PROXY.
Defaults env_keep +="HTTP_PROXY" Defaults env_keep +="HTTPS_PROXY"
So every time you use the sudo command, the HTTP_PROXY and HTTPS_PROXY are kept.
- StackOverflow – How to keep Environment Variables when Using SUDO
- Super User – How to allow user to preserve environment with sudo?