Foreground and Background

The built-in command fg brings a background job into the foreground. Normally this means that the job will have control of your terminal or window and therefore will be able to accept your input. In other words, the job will begin to act as if you typed its command without the &.

If you have only one background job running, you can use fg without arguments, and the shell will bring that job into the foreground. But if you have several jobs running in the background, the shell will pick the one that you put into the background most recently. If you want some other job put into the foreground, you need to use the job's command name, preceded by a percent sign (%), or you can use its job number, also preceded by %, or its process ID without a percent sign. If you don't remember which jobs are running, you can use the command jobs to list them.

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

A few examples should make this clearer. Let's say you created three background jobs as above. Then if you type jobs, you will see this:

[1]   Running                 alice &
[2]-  Running                 duchess &
[3]+  Running                 hatter &

jobs has a few interesting options. jobs -l also lists process IDs:

[1]     93 Running                 alice &
[2]-   102 Running                 duchess &
[3]+   104 Running                 hatter &

The -p option tells jobs to list only process IDs:

93
102
104

(This could be useful with command substitution; see Task 8-1.) The -n option lists only those jobs whose status has changed since the shell last reported it—whether with a jobs command or otherwise. -r restricts the list to jobs that are running, while -s restricts the list to those jobs which are stopped, e.g., waiting for input from the keyboard.[4]Finally, you can use the -x option to execute a command. Any job number provided to the command will be substituted with the process ID of the job. For example, if alice is running in the background, then executing jobs -x echo %1 will print the process ID of alice.

If you type fg without an argument, the shell will put hatter in the foreground, because it was put in the background most recently. But if you type fg %duchess (or fg %2), duchess will go in the foreground.

You can also refer to the job most recently put in the background by %+. Similarly, %- refers to the next-most-recently backgrounded job (duchess in this case). That explains the plus and minus signs in the above: the plus sign shows the most recent job whose status has changed; the minus sign shows the next-most-recently invoked job.[5]

If more than one background job has the same command, then % command will distinguish between them by choosing the most recently invoked job (as you'd expect). If this isn't what you want, you need to use the job number instead of the command name. However, if the commands have different arguments, you can use %? string instead of % command. %? string refers to the job whose command contains the string. For example, assume you started these background jobs:

$ hatter mad &[1]     189
$ hatter teatime &[2]     190
$

Then you can use %?mad and %?teatime to refer to each of them, although actually %?ma and %?tea are sufficient to uniquely identify them.

Table 8-1 lists all of the ways to refer to background jobs. Given how infrequently people use job control commands, job numbers or command names are sufficient, and the other ways are superfluous.

Table 8-1. Ways to refer to background jobs

Reference

Background job

%N

Job number N

%string

Job whose command begins with string

%?string

Job whose command contains string

%+

Most recently invoked background job

%%

Same as above

%-

Second most recently invoked background job