Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the environment variables using environ

How to print the environment variables in a C program using environ.

extern char **environ
like image 365
nitin_cherian Avatar asked Nov 27 '10 10:11

nitin_cherian


2 Answers

#include <unistd.h>
#include <stdio.h>

extern char **environ;
//...

int i = 0;
while(environ[i]) {
  printf("%s\n", environ[i++]); // prints in form of "variable=value"
}
like image 79
sje397 Avatar answered Nov 13 '22 23:11

sje397


Do you mean

int main(int argc, char **argv, char **envp)
{
    while(*envp!=null) {
        printf("%s\n", *envp);
        envp++;
    }
    return 0;
}
like image 39
jim mcnamara Avatar answered Nov 13 '22 21:11

jim mcnamara