How Do Shell Commands Function?

The Role of the Shell: Bridging User Input and Command Execution in Linux

·

2 min read

How Do Shell Commands Function?

Photo by Lukas on Unsplash

Shell is a program that takes user inputs and passes them to the operating system. It provides an interface to accept commands and their arguments, invokes system calls, and runs other programs.

A Terminal program, such as iTerm2, is a GUI that interacts with the shell, such as Z shell. It accepts text commands and displays the output.

The above screenshot is from a terminal with a shell process in ready state, waiting for a user to enter a command through the keyboard.

Once the user types above command and hits the enter key, the shell searches a file named "cat" through a list of directories stored in $PATH environment variable, separated by ":" .

In our case, the file "cat" is stored in the directory "/usr/bin". The contents of the "/usr/bin" directory, as shown in the screenshot below, include the "cat" file. In Linux, we have files and processes. When "cat" is executed, a process is created with a unique number called a PID. This PID is used by system calls and other processes.

Now the shell executes the "cat" command and creates a child process. The shell process acts as the parent process for the new process created by executing the "cat" command.

After executing the "cat" command, the shell returns to the ready state to accept a new command.