I’m sure Diana Ross would have named so her song, had she been a huge *nix fun.
What are we talking about? Job control. It’s simple if you already know it, but today I learnt some people didn’t know about putting shell jobs in background or restoring them in foreground.
When you run a command in a shell, it will take over the shell input until it’s done. And that’s called foreground.
& at the end of the command line and the job will run in background, leaving the shell input free for other tasks. The job number will appear between square brackets.
To bring a foreground job in background, suspend it (
Ctrl-Z) and issue
To bring it back on foreground, type
To see the list of jobs running in background, use the builtin
To reference a job in particular, you can use
n is the job number. So you could do,
fg %3 or
Alternatively, you can use
name is the command name used (for instance if you do
man bash &, then you can refer to the job as
? can be used to partially match strings: in the previous example,
fg %?n or
fg %?m would have referenced
jobs‘ output also tells you something more than just the list of jobs. A
+ is appended to the current job (last job stopped) and a
- is appended to the previous job (i.e. that run before the current).
Guess what, you can use both signs to retrieve those jobs: enter
For reasons I can’t explain, also
%% refer to the current job, so watchout for typos.
% can also be used to run commands. In other words:
%2 & is equivalent to
bg %2 and
%3 is equivalent to