In all of the command line examples so far, once you have typed in a command and pressed Enter or Return the system seems to go away to perform your command. When your command has finished, the shell finally returns a prompt to you as an invitation to enter another command. Figure 1. shows why this is so.
As you can see, once the shell has started a new process and set your command running, it then puts itself to sleep to wait for your command to finish. Only when this has happened is a signal sent back to the shell to wake it up so that it can give you a prompt and look for your next command.
Because Linux is multi-tasking, there is no reason why the shell shouldn't be able to run at the same time as your command and hence return a prompt to you immediately, while your command gets on with its task (see Figure 2). This is very useful if, for instance, the command you run will take a long time to complete its task, as it allows you to get on with something else.
The decision as to whether the shell will wait for a command to finish, or return to you immediately with a prompt, is made by you when you run the command. All you do is to put an ampersand character (&) at the end of your command line if you do not want the shell to wait but, rather, to return a prompt straight away. This is called putting the command into the background:
$ ls -lR / >/tmp/ls.lR & [l] 341 $
Here the output from an ls command is being written to the file /tmp/ls.lR and, rather than wait while this happens, the command has been run in the background with the & character on the end of the command line. The -R option to ls produces a recursive directory listing that works its way down through all the subdirectories that can be reached from the starting point (/ here). It means that this command produces a sorted long listing of the entire directory hierarchy of the machine - quite a lengthy task. The numbers printed on the line after the command are displayed by the shell. The number in square brackets is the job number for this command line. This is described in the next section. The second number is the PID (Process Identification Number) for the command being executed, or the PID for the last command on the line, if there is a pipeline involved.
In general, commands that you run in the background, must not try to take any input from the keyboard because all the keyboard input will go to the shell, after it gives you a prompt. The default for any background commands that do try to take keyboard input is that their execution is stopped and all their activity is suspended.