0

I was trying to set shell variable name with help of perl script.

But it was not running fine.

system("variable_name=\"variable_value\"");
`variable_name=\"variable_value\"`;

Any one tell me why it was not working.

Actually my question was little bit different. I want to know how to set environment "setenv" with help of perl script. I have tried $ENV{"VARIABLE_NAME"} = "home\/path_1\/path_2\/path_3";

Then I fire command echo $VARIABLE_NAME then it is not giving me path that I set from perl script.

Thanks

5
  • 2
    Possible duplicates 1.. How to export a shell variable within a Perl script, 2.. Setting an environment variable through a Perl script, 3.. How do I set an environment variable in Perl? Commented Jun 13, 2016 at 8:28
  • Actually my question was little bit different. I want to know how to set environment "setenv" with help of perl script. I have tried $ENV{"VARIABLE_NAME"} = "home\/path_1\/path_2\/path_3"; Then I fire command echo $VARIABLE_NAME then it is not giving me path that I set from perl script. Thanks in advance in for your support. Commented Jun 22, 2016 at 10:27
  • It's still unclear what you want to do. I'd suggest adding a self-contained reproducer -- that is to say, including in your question code that both performs the assignment and uses that assignment, and providing both that code's actual output and its expected output. Commented Jun 22, 2016 at 17:02
  • Also, since it requires five reopen votes, you might want a new question that's clearly specified rather than trying to resuscitate this one. Commented Jun 22, 2016 at 17:07
  • Code in perl script: $ENV{"VARIABLE_NAME"} = "home\ /path_1\ /path_2\ /path_3"; system("echo $VARIABLE_NAME"); After running this perl script Expected Output: home\ /path_1\ /path_2\ /path_3 Actual Output: It will not print any thing Commented Jun 22, 2016 at 17:12

2 Answers 2

3

You have no need to run a new shell to set an environment variable; indeed, doing so is counterproductive, since the value doesn't outlive the shell in which it's assigned (except through any surviving children of that shell which may have inherited the value).

You can simply set an environment variable directly in your perl:

$ENV{"variable_name"} = "variable_value"

Any subsequent shell you start from your perl script will see this.

Sign up to request clarification or add additional context in comments.

4 Comments

If you wanted the environment variable(s) to persist in a new shell and not return from the Perl script, you could also do something like: system($ENV{'SHELL'}); kill 'KILL', getppid();
@LexScarisbrick, ...huh? system($ENV{'anything'}) is a horrible idea -- substituting data into a string then passed to sh -c is a recipe for shell injection vulnerabilities unless that variable's value has been carefully vetted. And I'm not at all clear what the code in your comment above is more generally trying to accomplish. If you want to fork off a subprocess, set an environment variable in it, and then exec a shell to replace that perl subprocess, better to make it explicit.
Fair point. I was making the (probably wrong) assumption that the intent of the question was, "What is the Perl equivalent of doing source file in a shell?" Setting environment variables inside Perl, calling system() to invoke a new shell and then killing the process that called Perl effectively does this. But. Like you mentioned, invoking a new shell shouldn't be based on an environment variable due to the security risks.
Actually my question was little bit different. I want to know how to set environment "setenv" with help of perl script. I have tried $ENV{"VARIABLE_NAME"} = "home\/path_1\/path_2\/path_3"; Then I fire command echo $VARIABLE_NAME then it is not giving me path that I set from perl script. Thanks in advance in for your support
1

It was working. The problem is that the next system command runs a new shell which doesn't know about variables set in the first shell.

system 'x=value; echo $x'; # Outputs "value".
system 'echo $x';          # Empty line - the shell with $x doesn't exist anymore.

5 Comments

Of course it doesn't. It's a completely different process. You need to make x an environment variable and set it in the parent process (i.e. inside the Perl process which is invoking system).
Actually my question was little bit different. I want to know how to set environment "setenv" with help of perl script. I have tried $ENV{"VARIABLE_NAME"} = "home\/path_1\/path_2\/path_3"; Then I fire command echo $VARIABLE_NAME then it is not giving me path that I set from perl script. Thanks in advance in for your support
@AshutoshRawal: Works for me. perl -we '$ENV{VAR} = q(a/b/c); system q(echo $VAR)'. Note that variables are interpolated in double quotes, and Perl doesn't know you meant a shell variable.
@Choroba thanks for reply I have tried as you mentioned. But after running perl script then on same terminal if I fire command echo $VARIABLE_NAME then it print nothing.In perl script echo $VAR is working but on terminal it is not working.
@AshutoshRawal: That's normal. Setting a variable in a child process can't change the variable's value in a parent process.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.