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

Garthus
2 min readNov 24, 2020

A Shell provides you with an interface to the Unix system, executes commands read from the standard input or from a file stdout.

Once executed, The shell starts to wait for your command. the Standard input is then processed by the getline() function, which as suggested by the name, gets the line delimited by the carriage return.

If you don’t write the command with its absolute path, the shell :

  • first looks for an alias.
  • checks if a builtin command exists.
  • tries to find the command ls on folder specified in your $PATH environnement variable.
  • Looks in the current directory if the binary of the name ls is present.

…and guess what… The shell will find it in /usr/bin/ls.

The commands on a shell are launched by a function of the exec family.

We will take execve as an example for the execution.

execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded.

Because you don’t want to launch a new shell manually for each command entered, the shell forks its process before executing the command using the fork() function, creating two processes : a parent and a child. The child process with its own PID, prints the result to the standard output if there is one and exits. The parent process waits for the child process to terminate thanks to the wait() function.

Leaving you a blank new line to enter a new command after the execution.

--

--