What Occurs When You Run a Command in Linux?

Most Linux users are often unaware of how the operating system works internally. You may have been running Linux commands on the shell for a long time, but have you ever wondered what goes on behind the scenes when you press? Enter?

At the end, you will have a brief understanding of how the shell processes the command you entered on Linux.

Processing the command

When you enter a command, the first thing the shell does is to "token" the entire command. The shell then looks for a program name that corresponds to the first token on the command line.

If it is not found in any of the directories in the search path defined in the $ PATH environment variable or in the local directory with the . Operator or it is not a shell alias or function, the shell will return an error. If it finds a valid command, the shell then goes through all of the other tokens and decides whether it is a variable, a shell parameter, or an argument to the command.

When the shell determines that it is a variable or parameter like the ~ Operator for the home directory, the shell extends them or replaces them with their original values ​​in the command.

When the shell has expanded parameters or variables, it passes the command string to the command and runs the program with its arguments. The shell does not determine whether any arguments are valid. That is the task of the command.

Run the command

If the shell starts another command, how does it return to the same command prompt you used earlier? The shell makes a copy of itself, a process called forking. This copy of the shell replaces itself with the command with all of the arguments previously processed. This is called "Exec" and the combined process is called "Fork-and-Exec".

For example, when you run the ls command, the shell process shares with the Fork() Method and create another shell instance. Of the two shell processes running on the system, the additional shell runs ls with the exec () Function that turns itself into an instance of the ls command.

Meanwhile, the original shell waits for the command to complete. For this reason, you can use the job control to stop jobs and have jobs run in the background in the shell.

Related: What is a process in Linux?

Report exit status

Linux commands report on their exit status whether they were executed successfully or not. As the name suggests, programs report their exit status when they finish executing. You do this through that $? Environment variable that contains the completion status of the last execution command.

Conventionally, an exit status of 0 means a successful execution, while anything other than 0 usually means an error. Depending on how your command prompt is configured, your shell may also display a non-zero exit status on the command line.

The above screenshot is an example showing a custom zsh prompt that shows an error exit status of 127 due to a command that was not there.

Now you know how Linux commands work

Now that you know how the Linux shell processes, branches, and executes a command, and how programs report their exit status, you can use the command line more effectively.

Several Linux shells are available to users free of charge. Although each of them does more or less the same job, they differ in many ways. You can try installing some of the shells on your system and decide for yourself which one suits you best.

Which Linux Shell is Best? 5 common mussels in comparison

Are you looking for a Linux bash shell alternative? Here is an overview of which Linux shell is best: tsh, fish, KornShell, and Z-Shell.

Continue reading

About the author

David Delony
(57 published articles)

David is a freelance writer living in the Pacific Northwest but originally from the Bay Area. He has been passionate about technology since childhood. David's interests include reading, watching quality TV shows and movies, retro games, and collecting records.

By David Delony

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!

Click here to subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *