bg Command

The bg command resumes suspended jobs and runs them in the background. It's part of job control in Linux shells, allowing you to manage multiple processes efficiently.

Syntax

bg [job_spec ...]

Description

The bg command places jobs in the background, resuming suspended jobs or continuing stopped jobs. When a job is running in the background, you can continue using the terminal for other commands.

Key features:

  • Resumes suspended jobs in background
  • Allows multitasking in terminal
  • Works with job control
  • Can specify specific jobs by job ID
  • Built-in shell command

Job Specification

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

Resume most recent suspended job

bg

Resumes the most recently suspended job in the background

Resume specific job by number

bg %1

Resumes job number 1 in the background

Resume multiple jobs

bg %1 %2 %3

Resumes jobs 1, 2, and 3 in the background

Resume job by command name

bg %vim

Resumes the job whose command starts with "vim"

Resume job containing string

bg %?backup

Resumes the job whose command contains "backup"

Complete workflow example

# Start a long-running command find / -name "*.log" 2>/dev/null # Press Ctrl+Z to suspend it ^Z [1]+ Stopped find / -name "*.log" 2>/dev/null # Resume in background bg [1]+ find / -name "*.log" 2>/dev/null & # Check job status jobs [1]+ Running find / -name "*.log" 2>/dev/null &

Complete example of suspending and resuming a job

Start command directly in background

sleep 300 &

Starts a command directly in background (alternative to bg)

Resume previous job

bg %-

Resumes the previous job (second most recent)

Check all jobs

jobs -l

Lists all jobs with process IDs

Job Control Workflow

  1. Start a command: Run any command normally
  2. Suspend: Press Ctrl+Z to suspend the job
  3. Background: Use bg to resume in background
  4. Check status: Use jobs to see all jobs
  5. Foreground: Use fg to bring job to foreground
  6. Kill: Use kill %n to terminate job

See also