From https://stackoverflow.com/a/29400598/156458
The other solutions I've seen here so far are based on some system definitions, but it's in fact possible to have
sudouse the currentPATH(with theenvcommand) and/or the rest of the environment (with the-Eoption) just by invoking it right:sudo -E env "PATH=$PATH" <command> [arguments]In fact, one can make an alias out of it:
alias mysudo='sudo -E env "PATH=$PATH"'(It's also possible to name the alias itself
sudo, replacing the originalsudo.)
Given that
-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.
I was wondering why the following doesn't work
sudo -E <command> [arguments]
?
Given that "PATH=$PATH" <command> [arguments] is also a command, I was wondering why the following doesn't work:
sudo "PATH=$PATH" <command> [arguments]
?
Thanks.