Search code examples
clinuxraspberry-pienvironment-variablesgetenv

Environment variable $QUERY_STRING is not being included with *envp[]


I have set an environment variable with the following command:

QUERY_STRING='This is my query string'

This is my program:

#include <stdio.h>
#include <stdlib.h>

void main (int argc, char *argv[])
{
      printf("%s\n", getenv("QUERY_STRING"));
}

And this is what I get when I run the program:

mantis@toboggan /testing/cgi_download/temp $ echo $QUERY_STRING; ./a.out
This is my query string.
Segmentation fault
mantis@toboggan /testing/cgi_download/temp $

It appears as though the environment variable is not being set, and so getenv() is returning a NULL.

I really don't know why this isn't working. Other variables like $PATH are available. How do I set this environment variable so that it's available to my program?

uname -a:

Linux toboggan 3.18.7+ #755 PREEMPT Thu Feb 12 17:14:31 GMT 2015 armv6l GNU/Linux

Solution

  • This shell command:

    QUERY_STRING='This is my query string'
    

    creates a shell variable, not an environment variable. Shell variables are local to the shell process, not passed on to child processes like your a.out. To make it an environment variable, you need to export it:

    export QUERY_STRING