环境变量 $QUERY_STRING 未包含在 *envp[] 中

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

我已经使用以下命令设置了一个环境变量:

QUERY_STRING='This is my query string'

这是我的程序:

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

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

这就是我 运行 程序得到的结果:

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

似乎没有设置环境变量,因此 getenv() 返回 NULL

我真的不知道为什么这不起作用。其他变量如 $PATH 可用。如何设置此环境变量以便我的程序可以使用它?

uname -a:

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

这个shell命令:

QUERY_STRING='This is my query string'

创建一个 shell 变量,而不是环境变量。 Shell 变量是 shell 进程的本地变量,不会像您的 a.out 一样传递给子进程。要使其成为环境变量,您需要导出它:

export QUERY_STRING

您需要导出变量,以便它传播到子进程中:

 export QUERY_STRING='This is my query string'