When I call execvp
, for example execvp(echo, b)
where b is an array of arguments for the command a, will changing this array later affect the execvp call made previously? When I try calling execp(echo, b), it ends up printing out (null) instead of the content inside of b. Can anyone point out why and what I have to do to pass the arguments correctly?
After you call exec()
or one if its relatives, your original program doesn't exist anymore. That means nothing in that program can affect anything after the exec()
call, since it never runs. Maybe you're not building your arguments array correctly? Here's a quick working example of execvp()
:
#include <unistd.h>
int main(void)
{
char *execArgs[] = { "echo", "Hello, World!", NULL };
execvp("echo", execArgs);
return 0;
}
From the execvp()
man page:
The
execv()
,execvp()
, andexecvpe()
functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by aNULL
pointer.
A common mistake is to skip the part about "The first argument, by convention, should point to the filename associated with the file being executed." That's the part that makes sure echo
gets "echo" as argv[0]
, which presumably it depends on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With