How to set an environment variable when using system() to execute a command?

前端 未结 3 1807
难免孤独
难免孤独 2021-01-19 05:41

I\'m writing a C program on Linux and need to execute a command with system(), and need to set an environment variable when executing that command, but I don\'t

3条回答
  •  北海茫月
    2021-01-19 05:48

    This should work:

    #include "stdio.h"
    
    int main()
    {
        system("EXAMPLE=test env|grep EXAMPLE");
    }
    

    outputs

    EXAMPLE=test

提交回复
热议问题