title: How to use exec() to execute commands in C++ and Linux?
body: <p>For an assignment, I am to write a program that will work as a command line interpreter, accepting any command, creating a new process with <code>fork()</code>, and using the child process to execute the command with <code>exec()</code>. The problem is I don't know how to use the <code>exec()</code> family of commands, and searching online has proven to be of little help. I am working in C++ on a Linux server at my school.</p>
  
  <p>From <a href="https://stackoverflow.com/questions/5769734/what-are-the-different-versions-of-exec-used-for-in-c">this</a> post, I think I want <code>execlp()</code>, but I am really not sure, so please correct me if I'm wrong. When I try this:</p>
  
  <pre><code>string s = "/bin/" + command;
  execlp(s, command, NULL);
  </code></pre>
  
  <p>just to see if it works with a simple command and no arguments, like <code>ls</code>, I cannot even get it to compile, receiving this error: </p>
  
  <pre><code>shell.cpp:52: error: cannot convert âstd::stringâ to âconst char*â for argument â1â to âint execlp(const char*, const char*, ...)â
  </code></pre>
  
  <p>when attempting to pass arguments as strings. Annoyingly, our textbook does the exact same thing with string literals, and works successfully, with the statement <code>execlp("/bin/ls", "ls", NULL);</code>.</p>
  
  <p>Clearly, I don't know how the function is meant to be used, so what I am asking is can someone offer advice or provide a resource as to which <code>exec()</code> I should use and how I can use it for this purpose?</p>