DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How Shell Executes `ls -l *.c`
Dereje Desta
Dereje Desta

Posted on • Updated on

How Shell Executes `ls -l *.c`


In this post we'll talk about how shell executes command ls -l *.c. Moreover, we'll also discus how it is implemented under the hood.

We strongly advise you to have at least fundamental knowledge of C programming language and Linux system programming before you continue.

Note that we skipped error checks and memory management for brevity. Refer to the project implemention.

When we open a shell(users perspective) it prompts us to enter a line of command and when we enter the command it executes our command and it prompts us to enter another command.

But there is more to this story. The following are simplified version of steps the shell takes.

1. Getline

The shell prints the prompt and waits for input. Let us assume we entered ls -l *.c. This is preformed using getline().

 while(1) {
    char *line = NULL;
    size_t len = 0, nline;
    printf("$ ");
    nline = getline(&line, &len, stdin);
    if(nline == -1) return 0;
    // continues here
}
Enter fullscreen mode Exit fullscreen mode

2. Tokenize

Then The shell splits this line of command into tokens. In our case ls, -land *.c. this uses strtok(command, DELIM) where DELIM is string consisting whitespaces character(spaces, tabs and etc.).

// continued
const char *DELIM =  " \t\a\r"
token = strtok(line, DELIM);
while(token != NULL) {
    tokens[pos++] = strup(token); // push to array of strings
    token = strtok(NULL, DELIM);
}
Enter fullscreen mode Exit fullscreen mode

3. Expansion

After that the shell looks for expansion in our case token *.c matches every file ending in .c. So tokens will became :

tokens = {"ls", "-l", "main.c", "util.c", "test.c"};
Enter fullscreen mode Exit fullscreen mode

4. Alias

The shell checks if the first token is aliased. if it is it replaces it with the original expanded version. In our case ls is aliased to ls --color=tty.

ls: aliased to ls --color=tty
/usr/bin/ls
/bin/ls
Enter fullscreen mode Exit fullscreen mode

5. Builtins

Then checks if the command is builtin(like exit, cd, help ... etc). ls is not builtin command.

6. PATH

If the command is not builtin command it searches the command in the PATH enviromental variable. ls is found in path /bin/ls.

7. New Process

After that Creates new process using syscall fork().

int status;
pid_t child_pid = fork();
if (child_pid == -1)
    return (1);

if (child_pid == 0) // if this is child process execute
{
    if (execve(tokens[0], tokens, __environ) == -1)
        return (1);
} else // if it is parent wait for child
    wait(&status);
Enter fullscreen mode Exit fullscreen mode

8. Execution

Finaly it executes it. It uses execve(tokens[0], tokens, env) system call. See step 7. and continues at step 1 again.

Conclusion

This is just simple illustration of how shell goes on executing commands like ls -l *.c. But now days shells has evolved to do much complex tasks like history, conditional statements, loops and any other things programming languages supports. It was quite journey learning shell and we do believe you can benfit be implementing those concepts in yourself. You can find our implementation in C here. Thanks ๐Ÿ‘

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. ๐Ÿ›