DEV Community

Cover image for How Do Shell Commands Function?
Mukesh
Mukesh

Posted on • Originally published at waywardquark.hashnode.dev

How Do Shell Commands Function?

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

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.

Image description
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.

Image description

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 ":" .

Image description

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.

Image description
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.

Image description

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

Top comments (0)