When you type ls *.c, what happens?

Elmustapha Abourar
5 min readApr 13, 2022

Co-authored by Mustapha Abourar, Alexandre Dutertre and Pierre Dureau.

When you type ls -l, what happens?

Before starting, what is a shell ?

Before graphical interfaces existed, developers needed a method to interact with a computer using comprehensible command lines. The first Unix Shell, called the Thompson Shell, was authored by Ken Thompson and was introduced in 1971. Thompson developed and implemented the original Unix operating system. It also created the B programming language, the direct predecessor of the C language. The Thompson shell was succeeded as the main Unix shell by the so-called bourne shell (Bash) in 1979. Using this shell / command line interface, we can communicate with our computer because the kernel will take our command lines and send them out to the operating system.

How it works.

In an interactive system, a shell program is run by the login process after the user is authenticated. Once the shell has initialised on its data structures and is prepared to start operating, the shell will clear the 25-line screen and then prints a prompt in the first few characters positions of the first line. The shell then expects the user to type a command line in reply to the prompt.

  1. An executable program which can be compiled binaries such as programs written in C, or programs written in scripting languages such as the shell, Python, etc.
  2. A command built into the shell itself. bash provides a number of commands internally called shell builtins. The cd command, for example, is a shell builtin.
  3. A shell function. These are miniature shell scripts incorporated into the environment.
  4. An alias. Commands that you can define yourselves, built from other commands.
Gif explaining how shell works.

Printing a prompt:

When the shell is started, it can look up the name of the machine on which it is running, and prepend this string name to the standard prompt character. Once the prompt string is determined, the shell prints it to stdout whenever it is ready to accept a command line.

Get a command line:

the shell performs a blocking read operation so that the process that executes the shell will be blocked until the user types a command line in response to the prompt. When the command has been provided by the user (and terminated with a NEWLINE character), the command line string is returned to the shell.

Parsing the command:

The first such word is treated as the command name, and subsequent words are treated as parameter strings.

looking for the file:

The shell provides a set of environment variables for each user — these variables are defined in the user’s .bash_profile or .bashrc file, though they can be modified at any time with the set command.

Prints the prompt again

Once the command is done the prompt will be show again.

What is an enviroment variable:

An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

The PATH environment variable is an ordered list of absolute pathnames that specifies where the shell should search for command files.

Preparing the parameters:

The shell simply passes the string parameters to the command as the argv array of pointers to strings.

Executing the command:

The process that is executing the shell must protect itself in case the executable file has a fatal error in it. The shell uses the system calls fork, execve, and wait.

  • fork. This system call creates a new process which is a copy of the colling process except that it has its own process id and its own pointers to shared kernel entities such as file descriptors. After fork() has been called, two processes will execute the next statement after the fork in their own address spaces — the parent and the child. If the call succeeds in the parent process, fork() returns the process id of the newly created child process, and in the child process, fork() returns a zero value
  • execve. This system call is used to change the program that the process is currently executing. It has the form
  • wait. This system call is used by a process to block itself until the kernel signals the process to execute again — for example, because on of its child processes has terminated. When the wait call returns as a result of a child process terminating, the status of the terminated child is returned as a parameter to the calling process.
  • After that we can describe with some detail what happen when you type ls -l in a command line
  • The shell then waits for the user to type a command line in response to the prompt. The command line could be a string such as
$ ls -l

terminated with newline character (‘\n’.) When the user enter a command line it looks for aliases, the shell’s job is to cause the OS to execute the command embedded in the command line.

Every shell has its own language syntax and semantics. In the bash shell, a command line has the form

command argument_1 argument_2 . . .

where the command to be executed is the first word in the command line and the remaining words are arguments expected by that command. The number of arguments depends on which command is being executed. For example, the directory listing command can be used with no arguments — — simply by typing “ls”, or it may have arguments prefaced by the “-” character, as in “ls -l” where “l” is an argument.

After this blog, we are providing you with a copy of the sample of our mini Shell project that we made. Thank you for taking the time to read this blog.
Stay tuned, we will post more articles as we get the opportunity.

--

--

Elmustapha Abourar

Software Engineer Student @Holberton School & Pro Dancers Choreographer DeeJay / Beat Maker