[Chapter 12] 12.2 Other Ways to Refer to Jobs
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:36:26Z

#

| Chapter 12 Job Control |

## 12.2 Other Ways to Refer to Jobs

If you have several jobs in the background, you can refer to them by job number, as listed by the jobs (12.1) command. For example:

>     % jobs
>     [1]  + Stopped              vi TODO
>     [2]  - Running              nroff -ms ch01
>     % kill %2
>     % fg %1

You don't need to look up the job number to select a job, though. Instead, you can specify a job by name. Simply specify the command name instead of the job number after the percent sign. For example, the commands above could have been issued as:

>     % kill %nroff
>     % fg %vi

If you use `%?`, you can specify any unique part of the job's command line. What the manual fails to point out is that if you do this, you may need to quote (8.14) the question mark, since it's also a shell wildcard. If you don't, you may get the message `No match`. You could type one of the following commands:

>     % kill %?ch01   No quoting (normal)
>     % kill %\?ch01   Quoted (in some cases)

to kill the nroff job shown in the example above.

There are a couple of other shortcuts as well. A job number by itself is the same as the fg command followed by that job number. Why type:

>     % fg %3

when:

>     % %3

will do?

You can put a stopped job into the background in a similar way. For example:

>     % %2 &

will put job number 2 into the background.

Of course, it's also true that typing `fg` or `bg` without a job number can save you time if there is only one job, or if you want to refer to the current job.

The only problem is that the current job isn't always what you expect (12.3).

- TOR

12.1 Job Control: Work Faster, Stop Runaway Jobs |  | 12.3 The "Current Job" Isn't Always What You Expect