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

Ashutosh Rawal
  • 301
  • 1
  • 4
  • 16
  • 2
    Possible duplicates 1.. [How to export a shell variable within a Perl script](http://stackoverflow.com/questions/2967762/how-to-export-a-shell-variable-within-a-perl-script), 2.. [Setting an environment variable through a Perl script](http://stackoverflow.com/questions/19192682/setting-an-environment-variable-through-a-perl-script), 3.. [How do I set an environment variable in Perl?](http://stackoverflow.com/questions/1747796/how-do-i-set-an-environment-variable-in-perl) – Håkon Hægland Jun 13 '16 at 08: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. – Ashutosh Rawal Jun 22 '16 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. – Charles Duffy Jun 22 '16 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. – Charles Duffy Jun 22 '16 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 – Ashutosh Rawal Jun 22 '16 at 17:12

2 Answers2

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 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();` – Lex Scarisbrick Jun 12 '16 at 21:14
  • @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. – Charles Duffy Jun 12 '16 at 21:25
  • 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. – Lex Scarisbrick Jun 12 '16 at 21:35
  • 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 – Ashutosh Rawal Jun 22 '16 at 16:57
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.
choroba
  • 231,213
  • 25
  • 204
  • 289
  • 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`). – user1934428 Jun 13 '16 at 06:13
  • 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 – Ashutosh Rawal Jun 22 '16 at 16:57
  • @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 Jun 22 '16 at 20:11
  • @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. – Ashutosh Rawal Jun 23 '16 at 09:27
  • @AshutoshRawal: That's normal. Setting a variable in a child process can't change the variable's value in a parent process. – choroba Jun 23 '16 at 10:21