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

Sebastián Paruma
6 min readNov 27, 2020
Writing the ls -l command in the shell.

Let’s understand what is a Shell

People who use computers are familiar with the graphical user interface (GUI) of the computer. You will use your mouse to click on the application or file you see on the monitor. Using the shell, you can interact with the computer using the command-line interface. Not the most beautiful thing, but once you get used to it, you will find it more convenient, fast, and easy to use. First, it can be difficult for beginners because there are too many commands to remember. It is very powerful, it allows you to save commands in a file and execute them together. You can easily automate repetitive tasks. You can use a program called Terminal or Command Prompt to access the shell. There are many different shells available, but the most common shell used today is bash (a Unix shell), a product of the GNU Project, and a free software alternative to the Bourne shell. Bash is the abbreviation of “Bourne-again shell” (Bourne-again shell), which is a pun for the Bourne shell it replaces.

Seashells.

What is special about a shell?

In short, a shell is a program that receives commands from the keyboard and passes them to the operating system for execution. It allows users to interact with the computer. It is called the shell because it is the outermost layer of the operating system kernel.

Kernel?

The kernel is a computer program, which is the core of a computer operating system. It manages all the resources of the computer, such as files, processes, I/O, and memory management. Therefore, the shell converts user-readable commands into commands that the kernel can understand.

Interacting with the shell process.

So, what happens when you type ls -l in the shell? On the surface, typing ls -l will display all files and directories in the current working directory. It also shows the respective permissions, owner, and creation date. Now, once you hit the “enter” key, the behind-the-scenes story is another matter.

Shell opens

The first thing that happens is that the shell checks the PS1 variable in the environment variable and uses that value to create your shell prompt. PS1 stands for prompt string 1 and defines the main prompt string. PS1 is the main prompt displayed before each command, and people choose to customize it. Therefore, the shell displays a prompt, prompting the user to enter a command. The default command prompt is a single character, usually $ or #.

After entering the command and pressing Enter, the shell will read the command ls -l.

Token.

The tokenizing part

Shell will use the function getline() to read a line from the standard input of the command line and store it in the buffer. What happens next is that the function strtok() will be used to separate (tokenize) individual strings, separated by spaces (delimiters). The strtok() function must be called repeatedly and the pointer is stored in the static variable of the last interrupt. This is one of the steps of parsing, that is, by following a set of rules (such as using separators) to analyze the string into multiple parts and divide them into smaller parts (tokens) so that they can be managed one by one, analyze and explain the film.

Special characters and aliases

The command line management program (Shell)reads the command ls -l from the STDIN of the getline() function and parses the command line into parameters passed to the program being executed. The Shell will then check whether ls has an alias associated with the command you entered. If it finds any alias, it will expand the alias before running the command and check whether the word of the entered command is a built-in command. Next, it will check for special characters, such as: “, ’, \, *, &, #, and run the logic associated with the special characters.

PATH

Then, the shell will look for the program file before checking the program in the PATH to see if the first word of the command is a built-in function. The $PATH variable is a list of directories that the Shell searches every time a command is entered. Use’=’ as a separator to parse the environment variable $PATH. Once the $PATH is determined, the directories in the $PATH will be marked and further parsed using’:’ as a separator. You can check all your environment variables by typing env into the shell program and look for your PATH variable.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin/:bi

fork — execve — wait

These system calls are executed to the kernel. In order to create a new process to run the command, the system calls fork() to create an exact copy of the parent process. This will create another process, which is now a child of the parent process. When a system call is made to fork(), the child will also load the same program as the parent. Therefore, the system call execve() is made, which performs the following tasks:

The operating system (OS) stops the repeated processes of the parent process and loads the new program (ls in this example), and loads the new program of ls. The system call execve() replaces part of the current process with the newly loaded program in the ls executable file.

System Call wait()

An important step in the process of getting the child’s command is to use the wait() system call. All you have to do is to wait for the child process to complete the command process, and then return to the shell’s command prompt. The absolute path of ls is /bin/ls, if the program is executable, the Shell will use the system call execve() to run and execute the program in the child process.

After all, after executing ls -l, the shell will execute the shutdown command by calling the environment variable prompt string “one” (PS1), release the memory, exit, and prompt the user again for the next input. Appears after the command process is over.

If the command cannot be found or the command fails, an error message will be displayed and the user will be prompted to enter the command again.

Reviewing everything discussed in this article, we must first understand what a shell is and how it interacts with the kernel. Understanding how the two complements each other helps to understand how it works. Functions such as getline() and strtok() are the critical steps for the shell to begin parsing (marking) the command you just entered. Commands such as ls, cd, exit, and env are built-in commands, which means they are part of the Shell program. It just boils down to using the environment variable $PATH to search, find, and then execute the command. Finally, the system calls fork(), execute(), and wait() are important steps for the kernel to perform tasks. All of these are great tools for building your own Shell, but you need to be creative and implement more built-in features, depending on how many features you want to have in the Shell. Maybe now you and the system administrator who read this article can appreciate all the hard work that “Magic Tool” has done for us.

--

--