Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your current script doesn't even change an environment variable in the Perl script itself. Rather, it invokes a shell as a subprocess; that shell process executes <code>. /root/.profile</code>, which updates <code>$LD_LIBRARY_PATH</code> only in that shell process.</p> <p>You can change an environment variable in a Perl script (more precisely, in the process running the Perl script) by updating <code>%ENV</code>:</p> <pre><code>$ENV{LD_LIBRARY_PATH} = '/'; # or some more reasonable value </code></pre> <p>As <code>perldoc -v %ENV</code> says:</p> <blockquote> <p><code>%ENV</code> The hash <code>%ENV</code> contains your current environment. Setting a value in "ENV" changes the environment for any child processes you subsequently "<code>fork()</code>" off.</p> </blockquote> <p>But that probably still won't do what you want; it won't (and can't) affect the environment of the process that invokes the Perl script (your interactive shell), only the Perl process itself and anything it invokes.</p> <p>I'll assume you want to update <code>$LD_LIBRARY_PATH</code> in your current interactive shell process. To do that, you can have you Perl script <em>print a shell command</em> that will update <code>$LD_LIBRARY_PATH</code>. Then, rather than simply running your Perl script, you can execute it and then evaluate its output. For example:</p> <pre><code>$ cat env.pl #!/usr/bin/perl use strict; use warnings; print "export LD_LIBRARY_PATH=/\n"; $ ./env.pl # just prints the command without executing it export LD_LIBRARY_PATH=/ $ eval $(./env.pl) # executes the command in the current shell $ echo $LD_LIBRARY_PATH / $ </code></pre> <p>This assumes that your current shell is bash or something similar.</p> <p>Another option: After modifying <code>%ENV</code>, your Perl script can invoke another command, even a new interactive shell. The new process will inherit its environment from the Perl script. This can be a bit cumbersome, though; for example, if the new process is an interactive shell, it won't inherit <em>unexported</em> variables or history from the parent shell.</p> <p>(One note, not directly related to your question: The fact that you're messing with <code>/root/.profile</code> implies that you're doing things as <code>root</code> (superuser). This can be dangerous. Use the <code>root</code> account (either by logging into it or via <code>sudo</code> only for things that actually need root privileges. For anything else, use a personal user account.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload