Shell 🐚 Inside The Ls -l Command

joel silva
3 min readApr 14, 2021

What does the command interpreter do when we press ls -l?

Operation mode

There is a possibility of using Shell in two different ways known as the interactive mode and the non-interactive mode.

1- Interactive mode:

This mode allows to put commands deliberately for infinite times at least until it is β€œexit” or β€œEOF”(Ctrl + d).

2- Non-interactive mode:

Unlike the previous mode, it only runs once and does not show the prompt or β€œ$” that you can see. The sh code differentiates between both modes by using the isatty() function that employing a file descriptor allows you to know which mode the user is referring to. To run the non-interactive mode, which only allows one command at a time, you can do the following:

Command fragmentation.

The previous pointer will now be traversed to fragment into arguments separated by a specific character (in this case the space character), so that now β€œβ€ would be β€œβ€ β€œβ€. This information can be stored in an array of pointers where would be corresponding β€œβ€.ls -lls-larray[0]ls

Execution.

When everything goes well with that is, that an executable has been found is when it should be carried out and executed. The shell executes the call to the system that creates a new child process of the father process, this allows, in the interactive mode, to keep in the program flow without finishing the general execution of the main program (father process).To execute the file, a system call is made by or its different formats, this way and the one defined by the ls file in the directory, the programs of the current directory are listed.stat()fork()execve()/bin

but what about -l?

For this case, β€œβ€ that is one of the different options of β€œβ€ would be in , and at the moment of using the system call to execute a program, it will ask for the array of arguments from where we initially pass the route, that is an array, and it will be in charge of passing the following arguments from to the program to be executed. Of course, if it admits them.-llsarray[1]array[0]

This not only happens with ls but also when we make the call to the gcc compiler, we open the Emacs, vi or vim editors, or even when we create files with touch command or read them with cat command. Of course, the arguments concerning with respect to the rest of the arrays must be accepted by them, otherwise, the call to the system for execution will return an error.

Return

If everything in the execution has ended correctly the process is finished with an , which returns us to the main flow or parent process, which cleans the memory allocations now unnecessary, makes a line break, and again puts the prompt (in interactive mode) waiting for a new command._exit(0).

--

--