0

I have read the following answer from @nos on how to implement the execlp command but I am unable to run the same specifically for ls -l $pwd using execlp. I have tried both execlp("ls","ls", "-l", "$pwd", (char *)NULL); and execlp("ls","ls", "-l", "pwd", (char *)NULL); but nothing seems to work. Any directions in this thought process would be really helpful

Thanks.

re3el
  • 735
  • 2
  • 12
  • 28
  • You need to call `getenv` if you want to use an environment variable in a C program. – Turn Feb 03 '18 at 19:15

1 Answers1

1

Those $... variable belongs to shell, not the OS internal. When in shell you type such variables, shell will transform them to their actual value and then call system call.

In C program, you need to do it by yourself:

#include <unistd.h>

int main() {
    char *cwd = getcwd(NULL, 0);
    execlp("ls","ls", "-l", cwd, (char *)NULL);
}

getcwd() will give you the current directory.

llllllllll
  • 16,169
  • 4
  • 31
  • 54