Search code examples
unixansi-c

Which shell will execute the cmd when popen is called


First of all, forgive my awful english.....

This is the prototype

FILE *popen(const char* cmd_string, const char* type);

Here is my question, the book says that when popen function is called, it will call exec to get a shell to execute the cmd_string we give to popen, but I'm not sure which shell will exec get, so can anyone give me an answer?


Solution

  • Let's try and see:

    $ cat test.c
    #include <stdio.h>
    
    int main() {
    
      FILE *fp;
      char var[5];
    
      fp = popen("echo $0", "r");
      fgets(var, 5, fp);
      printf("%s", var);
    }
    $ gcc -Wall test.c
    $ ./a.out
    sh