sudo 保持环境变量

sudo 之后环境变量就没了,但有个 -E 选项能够保持环境变量:

1
2
$ export HTTP_PROXY=foof
$ sudo -E bash -c 'echo $HTTP_PROXY'

帮助页面解释如下:

1
-E, --preserve-env

Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.