what happen when you type “ls -l “ command in a Linux shell

zitouni mehdi
4 min readNov 22, 2020

the Linux shell is a special interactive utility , it provide a way for user to start programs and manage processing runing on the Linux system. The core of the shell is the command prompt. The command prompt is the interactive part of the shell. It allows you to enter text commands from the keyboard, and then it interprets the commands and executes them in the kernel. The default prompt symbol for the bash shell is the dollar sign ($). This symbol indicates that the shell is waiting for you to enter text.

prompt start on terminal

Listing Files and Directories

ls command displays the files and directories in your current directory

display files and directories

in the basic listings the ls command doesn’t produce much information about each file. For listing additional information, another popular parameter is -l. The -l parameter produces a long listing format, providing more information about each file in the directory:

there is multiples steps that occurred

so after the prompt $ is displayed in the terminal:

1- shell read input argument typed by user

the shell use the getline() function to reads an entire line from stream, and return on success the number of characters read including the delimiter character and return -1 on failur.

here is the prototype of getline():

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

fore more details see (man 3 getline ) manual.

2- split string (arguments input stream)

arguments whould require that we parse the input lineptr and then pass each arguments as a separate parameter to the exec sys-call family (in our project we use execve() to execute program).

3- execution (execve, fork, wait)

to execute our program by calling execve we need to create an other process (the child) by using fork() to create child process by duplicating the calling process .the new process is refferd to as the child process. the calling process is refferd to as the parents process. On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent.

prototype: pid_t fork(void); (see man fork manual)

When a process calls one of the exec functions, that process is completely
replaced by the new program, and the new program starts executing at its main function.With fork, we can
create new processes; and with the exec functions, we can initiate new programs. the wait functions (man 3 wait)handle termination and waiting for termination.

fork process diagram

execve (system call):

prototype: int execve(const char *pathname, char *const argv[], char *const envp[]);

the executable file is searched for in the directories specified by the PATH environment variable The PATH variable contains a list of directories, called path prefixes, that are separated
by colons.

For example, the key=value environment string

String PATH environment

For example:usr/bin will be searched by appending usr/bin/ls.

Once it finds the binary for ls , the program is loaded in memory and a system call fork() is made. This creates a child process as ls and the shell will be the parent process. The fork() returns 0 to the child process so it knows it has to act as a child and returns PID of the child to the parent process.

exit : Terminates the process with an exit status.

three way to exit process ( ctrl +D, Ctrl +c or by write exit and type enter from keyboard)

--

--