What happens when you type ls -l in the shell

Carlos Daniel Cortez
3 min readJan 15, 2020

--

by: Felipe Satizabal & Carlos Daniel Cortez

We know that “ls” is one of the linux commands of the bin folder, and “-l” is a flag that defines the specific behavior of the command. and we know that the output will be a list of the files of the current directory with their respective information. But the process is even more interesting, let me explain the reason.

The first thing we have to know is that shell is a command interpreter. To understand the shell in a better way we make our own shell. To do this, it was necessary to define the following general program actions from the beginning:

  1. Read the information that the user writes in the command terminal.
  2. Organize the information received.
  3. Perform validations before generating a thread.
  4. Perform more validations.
  5. Execute the command

In our case we use the following functions:

chdir(), close(), execve(), exit(), fork(), free(), getcwd(), getline, isatty,   malloc(), open(), perror(), read(), signal(), stat(), strtok(), wait(), write().

Although we had to use our own version of the following functions:

strcat(), strcmp(), strcpy(), putchar()

and of course we did other functions but they are part of personal logic.

so how does shell work?

In the case of “ls -l”, the shell captures the information as arguments in an array of char pointers, which in other languages ​​is an array of strings. The zero position of the array is identified as the position of the command and the following positions as the arguments of that command.

Note: for the above we use:
getline(): to get the information.
malloc(): reserve dynamic space for the arguments.
strtok(): divide the information.
isatty(): to identify the source of the information.
execve(): execute the command.
free(): to free memory.

Then we create a thread for the command. In that thread we get the environment variables and test the command with a given path especially with “PATH”. If at any time a valid route is found. We use the path to execute the command. Finally the thread ends and the main process continues.

Note: for the above we use:
fork(): to create the thread.
wait(): to make the process wait for the end of the thread.
malloc(): reserve dynamic space for the tokens.
strtok(): divide the information in tokens.
strcat(): to concatenate “/ls”.
stat(): to verify if the file exists.
free(): to free memory.
exit(): to end the thread

Extra information

  • PATH is an environment variable whose value can be accessed using $PATH.
  • There are commands called “built-in commands” that are not found inside that are executed directly in the shell itself. Example: cd, help, etc
  • We can print the information of the environment variables with the env command.
  • The cd command uses the chdir() function to change the current address.
  • Shell has exit codes defined as 1, 2, 126, 127, 128 and 130. You can see more information in this link http://www.tldp.org/LDP/abs/html/exitcodes.html
shell location in the system
Environment Variables

--

--

Carlos Daniel Cortez

Multimedia Engineer, Full Stack Software Engineer, Full Stack Web Developer.