fg Command

The fg command brings background or suspended jobs to the foreground, giving them control of the terminal. It's essential for job control in Linux shells, allowing you to switch between multiple processes.

Syntax

fg [job_spec]

Description

The fg command places jobs in the foreground. When a job is in the foreground, it has control of the terminal and can receive keyboard input. This is the opposite of the bg command which runs jobs in the background.

Key features:

  • Brings background jobs to foreground
  • Resumes suspended jobs in foreground
  • Gives job control of terminal
  • Allows interaction with the process
  • Built-in shell command

Job Specifications

Format Description
%n Job number n
%string Job whose command begins with string
%?string Job whose command contains string
%% or %+ Current job (most recent)
%- Previous job

Examples

Bring most recent job to foreground

fg

Brings the most recent background or suspended job to foreground

Bring specific job to foreground

fg %1

Brings job number 1 to the foreground

Bring job by command name

fg %vim

Brings the job whose command starts with "vim" to foreground

Bring job containing string

fg %?editor

Brings the job whose command contains "editor" to foreground

Bring previous job to foreground

fg %-

Brings the previous job (second most recent) to foreground

Complete workflow example

# Start a text editor vim document.txt # Suspend it with Ctrl+Z ^Z [1]+ Stopped vim document.txt # Start another process in background find / -name "*.log" 2>/dev/null & [2] 12345 # Check jobs jobs [1]+ Stopped vim document.txt [2]- Running find / -name "*.log" 2>/dev/null & # Bring vim back to foreground fg %1 # Now you can continue editing the document

Complete example of suspending and resuming a text editor

Interactive session management

# Start multiple processes top & [1] 12346 htop & [2] 12347 # Check what's running jobs [1]- Running top & [2]+ Running htop & # Bring htop to foreground for interaction fg %htop # Now you can interact with htop # Suspend htop with Ctrl+Z, then bring top to foreground fg %top

Managing multiple interactive programs

Development workflow

# Start code editor code project/ & [1] 12348 # Start development server npm start & [2] 12349 # Start database mongod & [3] 12350 # Bring editor to foreground when needed fg %code # Switch to server logs fg %npm

Typical development environment job management

Resume suspended process

# Start a long-running command rsync -av /large/directory/ /backup/ # Accidentally suspend with Ctrl+Z ^Z [1]+ Stopped rsync -av /large/directory/ /backup/ # Resume in foreground fg # Process continues where it left off

Resuming accidentally suspended processes

Job Control Workflow

  1. Start processes: Run commands normally or with &
  2. Suspend: Press Ctrl+Z to suspend foreground jobs
  3. Check jobs: Use jobs to see all jobs and their status
  4. Background: Use bg to resume jobs in background
  5. Foreground: Use fg to bring jobs to foreground
  6. Terminate: Use kill %n to terminate specific jobs

Common Use Cases

  • Text Editors: Switch between multiple editing sessions
  • Interactive Programs: Bring monitoring tools to foreground
  • Development: Switch between editor, server, and database
  • System Administration: Manage multiple system tasks
  • File Operations: Resume suspended file transfers or copies

See also