Home > Enterprise >  Execvp command not working with "type" command
Execvp command not working with "type" command

Time:01-28

I've been trying to execute the "type" command with execve funtion in C

char *arg[] ={
    "type",
    "type",
    NULL
};
   execvp("type", arg);

Here is the code that i've been using but it returns me -1 I've tried approximatively the same code for echo command , it working perfectly

Any help please?

CodePudding user response:

This is normal and expected behavior: type is a shell builtin, not an external command; only external commands can be invoked with execv-family syscalls.

You could invoke a shell, and have that shell run type:

/* cmd should be the command you want to check 'type' output for */
char *args[] = { "bash", "-c", "type $@", "bash", cmd, NULL };

...or you could use which instead (which is generally less capable, but the extra capabilities type adds in bash are things that intrinsically require a shell).

  •  Tags:  
  • Related