title: linux execvp ; ls cannot access |, No such file or directory
body: <p>I am trying to code a shell. But my shell doesn't execute the command - ls -l | less. I am using execvp. the code is given below.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int main(){
      int pid, status, num, len;
      char str[1000], cwd[100];
      char* word[100];
  
      getcwd(cwd, sizeof(cwd));
  
      while(1){
          chdir(cwd);
  
          printf("%s &gt; ", cwd);
  
          gets(str);
  
          pid=vfork();
  
          if(pid == 0){
              num = 0;
              word[num] = strtok (str, " ");
  
              while (word[num] != NULL) {
                  word[num] = strdup (word[num]);
                  len = strlen (word[num]);
                  if (strlen (word[num]) &gt; 0)
                      if (word[num][len-1] == '\n')
                          word[num][len-1] = '\0';
                  word[++num] = strtok (NULL, " ");
              }
  
              if(strcmp(word[0], "cd") == 0){
                  chdir(word[1]);
                  getcwd(cwd, sizeof(cwd));
              }
              else{
                  execvp(word[0],word);
              }
  
              exit(0);
          }
          else{
              wait(&amp;status);
          }
      }
  
      return 0;
  }
  </code></pre>