1

Under linux, I have a bash script, that launches a c++ program binary. What I need to do is set an environment variable in that script, and access that variable inside the launched C++ program using getenv .

Here is the code for the script

#!/bin/bash
export SAMPLE_VAR=1
./c++_binary

The c++ program:

char * env_var = getenv("SAMPLE_VAR");
if (env_var != NULL) printf("var set\n");

However this does not seem to work. From what I understand is that when we execute the script, it will run in a new subshell and set the environment variable SAMPLE_BAR there, but the C++ binary is launched in the same subshell as well (may be I am wrong here) so it should have access to the SAMPLE_VAR. I even tried writing a separate script that just sets the env variable, and in the main script I called that script as source env_var_set.sh to no avail.

Is it possible to pass on a newly set environment variable to a program this way ? Thanks

Abdullah
  • 521
  • 2
  • 8
  • 20
  • 2
    Did you mean `char * env_var = getenv("SAMPLE_VAR")`? Extra quotes can make all the difference. – mvp Jun 11 '13 at 09:02
  • Have you tried `int main(int argc, char **argv, char **envp);` also? The `envp` should capture environment varibales as well. – user2052561 Jun 11 '13 at 09:04
  • @mvp, yes sorry, I did put in the quotes, edited the quest. – Abdullah Jun 11 '13 at 09:05
  • 2
    this is weird - you are doing it correctly, and it should just work. About the only reason why it would not if you have some security software at work, something like [AppArmor](http://stackoverflow.com/questions/5835664/how-does-apparmor-do-environment-scrubbing). – mvp Jun 11 '13 at 09:11
  • When you say "it doesn't work", is `getenv` returning `NULL`? – chepner Jun 11 '13 at 09:37
  • Yes it returns NULL, i did a system("printenv") inside the program as well. And the SAMPE_VAR isnt set in the environment of the program. – Abdullah Jun 11 '13 at 09:43
  • 1
    Your question mentions SAMPLE_VAR and SAMPLE_BAR; your last comment mentions SAMPE_VAR. You have to get your spelling consistent. Your code should work fine. Your script could use: `SAMPLE_VAR=1 ./c++_binary` too, which exports the variable only to the one program. – Jonathan Leffler Jun 11 '13 at 12:46
  • Ah Sorry for the Typos, And my mistake, Inside the script I was launching the binary with 'sudo' which ran it in root's env and didnt have the variable set there. Removed sudo and it worked fine. Sorry for the confusion. Cheers. – Abdullah Jun 11 '13 at 17:14

2 Answers2

1

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

Ah Sorry for the Typos, And my mistake, Inside the script I was launching the binary with 'sudo' which ran it in root's env and didnt have the variable set there. Removed sudo and it worked fine. Sorry for the confusion. Cheers. – Abdullah

Community
  • 1
  • 1
Armali
  • 18,255
  • 14
  • 57
  • 171
-1

First of all you need to source your shell script in order for the env variable to be set. and secondly include quotes in the getenv call.

char * env_var = getenv("SAMPLE_VAR");
if (env_var != NULL) printf("var set\n");
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • He's exporting the variable and starting the binary from the same script. Sourcing versus executing the script won't make any difference. – chepner Jun 11 '13 at 09:36