Your current script does not even change the environment variable in the Perl script itself. Rather, it calls the shell as a subprocess; this shell process performs . /root/.profile . /root/.profile , which updates $LD_LIBRARY_PATH only in this shell process.
You can change the environment variable in a Perl script (more precisely, while working with a Perl script) by updating %ENV :
$ENV{LD_LIBRARY_PATH} = '/';
As perldoc -v %ENV says:
%ENV hash contains the current environment. Setting the value to "ENV" changes the environment for any child processes that you subsequently " fork() " are disabled.
But it probably won't do what you want anyway; it will not (and cannot) affect the process environment that calls the Perl script (your interactive shell), only the Perl process itself and everything that it calls.
Suppose you want to update $LD_LIBRARY_PATH in the current interactive shell mode. To do this, you can force the Perl script to print a shell command that will update $LD_LIBRARY_PATH . Then, instead of just running your Perl script, you can execute it and then evaluate its output. For instance:
$ cat env.pl
This assumes your current bash shell or something similar.
Another option: after changing %ENV your Perl script may invoke another command, even a new interactive shell. The new process inherits its environment from the Perl script. This can be a little cumbersome; for example, if the new process is an interactive shell, it will not inherit outstanding variables or history from the parent shell.
(One point not directly related to your question: the fact that you /root/.profile with /root/.profile implies that you are doing something as root (superuser). This can be dangerous. Use root (or by logging in , or through sudo only for things that really need root privileges, use a personal user account for anything else.
source share