Most Linux users are often unaware of the internal working of the operating system. You might be running Linux commands on the shell for a long time, but have you ever wondered what's happening behind the scenes when you hit Enter?

By the end, you'll have a brief understanding of how the shell processes the typed command in Linux.

Processing the Command

When you enter a command, the first thing the shell does is break the entire command into "tokens." The shell will then look for a program name belonging to the first token in the command line.

If it doesn't find it 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's not a shell alias or shell function, the shell will give an error. If it does find a valid command, the shell will then go through each of the other tokens and decide if it's a variable, a shell parameter, or an argument to the command.

If the shell determines that it's a variable or a parameter like the ~ operator for the home directory, the shell will expand them, or replace them with their original values in the command.

When the shell has expanded any parameters or variables, it will pass along the command string to the command, running the program with its arguments. The shell doesn't determine if any arguments are valid. That's the command's job.

Running the Command

When the shell launches another command, how does it come back to the same prompt you were using before? 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 that were processed earlier. This is known as an "exec," and the combined process is known as "fork-and-exec."

For example, when you run the ls command, the shell process will fork itself using the fork() method and create another shell instance. Out of the two shell processes running on the system, the additional shell will execute ls using the exec() function, transforming itself into an instance of the ls command.

Meanwhile, the original shell waits for the command to complete. This is why you can use job control to suspend jobs and have jobs run in the background in the shell.

Related: What Is a Process in Linux?

Reporting the Exit Status

Linux commands report whether they ran successfully or not through their exit status. As the name suggests, programs report their exit status when they finish running. They do this through the $? environment variable, which contains the exit status of the last run command.

By convention, an exit status of 0 indicates a successful execution, while anything other than 0 usually means an error. Your shell might also indicate a non-zero exit status on the command line depending on how your prompt is configured.

error code in kali linux

The above screenshot is an example showing a customized Zsh prompt that shows an error exit status of 127 due to a command that doesn't exist.

Now You Know How Linux Commands Work

Now that you're aware of how the Linux shell processes a command, forks and execs itself, and how programs report their exit status, you can make use of the command line more effectively.

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