The Shell and UNIX - Command execution
(Page 4 of 4 )
So how does a command's execution happen? In the case of built-in commands, this will be done by the running shell. If the tool/command is inside an external binary file, then something else happens. The current shell we are just using inside the terminal will start a new shell. This will get the content of the command line as input.
While the child shell completes its command, the father shell will wait. Therefore, we will not have the prompter string. The child shell will inherit the input/output files and the environment variables. While a running shell starts, the operating system will store some important data related to this. All of this is stored inside what we call the process. The shell itself is a process.
On the command line, there are two kind of separating characters: the new line and the ;. On a single command line we can write more commands separated with the ; , however these will be executed one after another and not at the same time.
The command we start we describe as running in the foreground, as it will not give back the prompter and will print on the current shell terminal. We can also run the process/command in the background. If this is the case, the shell will give back the prompter and allow us to start multiple commands running at the same time.
There are two ways to achieve this. The first is to add to the end of the command line the & character. For instance, the sleep command will force the terminal to wait the given number of seconds if started in the foreground. Nevertheless, if we add to the end of the command the & character, the child shell will still wait for the given seconds; but during that we can make further commands, as we will have the prompter.
sleep 2 &;
The second option is to start the command line as usual, and suspend it with the Ctrl + Z key combination. With the help of the bg command, we can now start this in the background. You can bring back to the foreground a background process with the help of the fg command.
UNIX systems assign to every process an integer number to make it easy to work with them. When we send to the background or bring to the foreground a command line, this is what we will get back. We can use this to choose which of the commands to bring to the foreground if we have multiple commands in the background. Here is example from the terminal:

Naturally, the commands can work for your own shell scripts, not just the ones already existing in the shell. While an echo may run fast enough so you should not worry about running it in the background, a more complicated script can have run times well beyond the insensible one-second boundary.
There is a lot more to know about the processes and tasks, however we will work with this later. For now, this will be enough for you to understand the general concepts of how a command works on the shell.
This is all for today. In the hope that you enjoyed and learned a lot from this article, I invite you to come back next week when we will observe the shell variables, the quotes, language packs, how to customize your shell to start as you wish and other interesting subjects about the shell. I would like also to ask you to rate my article and if you have any kind of question just ask it in the blog that you will find at the end. Live With Passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |