I’m experiencing a strange behavior related to environment variable preservation with sudo on macOS(15.2): sudo -E
doesn't preserve the environment variable PERL5LIB
.
Reproduction Steps
Set an environment variable in the shell:
export PERL5LIB=foo
.
The command
env | grep PERL5LIB
outputs as expected:
PERL5LIB=foo
.
However, the command
sudo -E env | grep PERL5LIB
,
returns nothing.
In contrast, this works:
sudo --preserve-env=PERL5LIB env | grep PERL5LIB
.
It successfully preserves PERL5LIB
.
This is confusing because I expected sudo -E
to preserve all environment variables from the invoking shell, including PERL5LIB
. But clearly, it’s not working that way for this particular variable.
Question
PERL5LIB
not preserved by sudo -E
on macOS?PERL5LIB
, or does it affect other variables as well?PERL5LIB
is preserved when using sudo?You can run :
$ sudo strings /usr/bin/sudo | grep PERL
PERLIO_DEBUG
PERLLIB
PERL5LIB
PERL5OPT
PERL5DB
They are considered potentially dangerous and removed by default.
I think the command you gave sudo --preserve-env=PERL5LIB env
is the most secure, reliable way to ensure that PERL5LIB is preserved when using sudo.