0
int main(void)
{
    execl("echo", "test");
    return 0;
}

I want to execute command echo test with execl Why ? Because i can't use system() i have some reasons What is wrong ?

Starrrks_2
  • 115
  • 1
  • 8
  • 1
    Don't know what is wrong, sorry, but have you checked out the man pages for `execl` for usage information? – user4581301 Apr 30 '16 at 00:20
  • You need to read the manual really carefully to figure out which of the `execxxx` functions is best suited to your needs and what parameters they accept. You may prefer `execlp` like: `execlp("echo", "echo", "test", nullptr);`. That checks the system `PATH` to find the executable. – Galik Apr 30 '16 at 00:36

1 Answers1

3

The execl function does not look up commands on your PATH like a shell would, so you need to provide the full path to echo (or else provide a relative path from your current working directory, I think). Also, the first arg in the args list should be the filename of the executable, and the last arg should be NULL so that execl can figure out how many args you are trying to pass.

This works for me:

#include <unistd.h>

int main(void)
{
    execl("/bin/echo", "/bin/echo", "test", NULL);
    return 0;
}

You can run which echo to find out where echo is located on your system; it might be different from mine and you would have to edit the code.

David Grayson
  • 84,103
  • 24
  • 152
  • 189