anthony huggins
2 min readAug 29, 2019

What happens when you type ls -l in the shell?

Shell programs allow you to run programs on your computer from the command line. When ls -l is entered the shell program command prompt, the shell will try to find the ls program. It does this by looking in all PATH directories for the ls program. The PATH is one of many environment variables that your computer shares with the shell.

The PATH env variable is a list of directories where programs can be

It will check if each of these paths contain an executable file named ls. This can be done with the System call Access().

if (access(command, X_OK) == 0)

In the example above command is a string congaing whatever the program is named, in this case ls, and X_OK is a macro that will tell access to look for only executable files. Now that the shell knows the location of the program we want to run it needs to actually run it. In order to do this it needs to create a new process so that input prompt will print again after the program runs.

pid = fork();if (pid < 0)
{
perror(name);
}
if (pid > 0)
{
wait(NULL);
}
if (pid == 0)
{
if(execve(location, input, NULL) == -1)
perror(name);
}

In the example above the shell makes a child process and runs the program in that process. It runs the program using the execve() system call. The input parameter represents the name of the program(ls) as well as any other arguments (-l). The ls program then takes over the child process and prints all files of the current directory in long form. Once the child is finished the parent resumes and prints another prompt for your next command.

anthony huggins
anthony huggins

No responses yet