Search code examples
clinuxcommand-linesystem

System function is not working in C when the command contains ".."


This is my code:

#include<stdlib.h>
int main()
{
    system("getent passwd {1000..60000}");

    return 1;
}

I believe the ".." present in the command is causing the problem cause the program runs properly for other commands.


Solution

  • system does not run your normal shell. It instead always runs /bin/sh. From system(3):

    DESCRIPTION

    The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:

              execl("/bin/sh", "sh", "-c", command, (char *) NULL);
    

    system() returns after the command has been completed.

    Usually /bin/sh is a shell that does not understand {1000..60000}. To run bash or zsh you need to do something like

    system("/bin/bash -c 'getent passwd {1000..60000}'");