System and processes

The fg command

Brings a background or stopped job back to the foreground, where it gets your keyboard.

Most useful options

%nJob number, such as %1. It is the one jobs shows in brackets.
%% o %+The current job: the last one started in the background, stopped or resumed. It is the one used with no argument.
%-The job before the current one.
%textoA job whose command starts with that text, such as %sle for sleep 100. If several start the same way, bash reports an error.
%?textoA job whose command contains that text anywhere, such as %?100.

Copy-ready examples

$ jobslists the jobs of this shell before bringing one back. With no jobs, it prints nothing.
$ fgbrings the current job, the last one you started in the background.
$ fg %1brings job number 1 to the front.
$ fg %?100brings the job whose command contains 100, without looking at its number.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: fg: current: no such jobThere are no jobs in this shell: they finished or were never started. A job is created with & or by suspending a command with Ctrl+Z.Run jobs to check, then start or suspend the command again.
bash: fg: no job controlThe shell has job control off, which is typical in a script. Bash turns it on by default only in interactive shells.If you really need fg in a script, run set -m at the start.
bash: fg: %2: no such jobThat number does not exist: the job finished or never had that number.Check jobs and use the number shown in brackets.

Typical mistakes

  • A background job that tries to read from the keyboard stops by itself (SIGTTIN). With fg you bring it to the front and it can read again.
  • fg only sees the jobs of this shell: if you closed the terminal where you started the command, it does not appear here.
  • Once in the foreground, Ctrl+C sends SIGINT to the job and usually ends it. To keep it, suspend it with Ctrl+Z and then use bg.

Differences from similar commands

  • bg: resumes a stopped job but leaves it in the background, without your keyboard. fg brings it to the front.
  • jobs: only lists the jobs and their state. fg acts on one of them.
  • kill: sends signals to a process or to a job with %N. fg sends no signals: it brings the job to the front.

Related commands

FAQ

How do I get the prompt back without cancelling a long command?

Press Ctrl+Z: the command is suspended and you get the prompt back. Then use bg to keep it running in the background, or fg to resume it in front.

What happens with fg if the job already finished?

It is no longer in the job table, so fg finds nothing and fails with a no such job error. Nothing else happens.

Can I bring the last job to the front without typing a number?

Yes: fg with no arguments brings the current job, the last one started in the background, stopped or resumed.

More System and processes commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course