bg
Move a stopped job to background
TLDR
Resume the most recently suspended job and run it in the background
Resume a specific job and run it in the background (run jobs to find the job number)
SYNOPSIS
bg [%job_spec]
This command is a shell built-in; it does not have options in the traditional sense. The optional %job_spec refers to the job to be acted upon.
PARAMETERS
%job_spec
An optional argument specifying the job to be resumed. If omitted, the most recently stopped job is used. Job specifications can be numbers (e.g., %1), prefixes of command names (e.g., %vi), or special symbols (e.g., %% or %+ for the current job, %- for the previous job).
DESCRIPTION
The bg command (short for "background") is a shell built-in that resumes a stopped job and continues its execution in the background. When a command is running in the foreground, you can suspend it by pressing Ctrl+Z. This sends a SIGSTOP signal to the process, pausing its execution. Once suspended, you can use bg to make it continue running without occupying your terminal's input prompt. This is particularly useful for long-running tasks that don't require immediate interactive input. You can identify stopped jobs using the jobs command, which lists all active jobs with their status and job ID. If no job is specified, bg operates on the most recently stopped job. It's often used in conjunction with fg (foreground), which brings a background or suspended job back to the foreground.
CAVEATS
bg is a shell built-in command, meaning its behavior is handled directly by your interactive shell (like Bash, Zsh, or Ksh) rather than being a separate executable program. Consequently, its exact implementation details might vary slightly between shells, though its core functionality remains consistent.
It can only be used on jobs that are currently stopped (suspended) or running in the foreground. It cannot be used on jobs that have already terminated or are already running in the background. Output from a backgrounded job will still appear on the terminal unless redirected.
JOB CONTROL
The bg command is an integral part of the shell's job control system. Job control allows users to manage multiple processes within a single interactive shell session, enabling them to suspend foreground jobs, move them to the background, bring background jobs to the foreground, and list active jobs. This system provides a powerful way to multitask efficiently on the command line.
JOB SPECIFICATION (<I>%JOB_SPEC</I>)
When referring to a specific job with bg (or fg, jobs, kill), you use a job specification. Common forms include:
- %N: The job with number N (as shown by jobs). E.g., %1.
- %string: The job whose command line begins with string. E.g., %emacs.
- %?string: The job whose command line contains string. E.g., %?ssh.
- %% or %+: The current job (the one most recently started or stopped).
- %-: The previous job.
HISTORY
Job control, including the bg command, was a significant enhancement to Unix-like operating systems to improve interactivity and multitasking for command-line users. It was first widely implemented in the C shell (csh) in the late 1970s and subsequently adopted by other popular shells like the Korn shell (ksh) and later Bash. Its development was driven by the need to manage multiple processes within a single terminal session without having to open new terminal windows or use complex process management tools. It has been a standard feature in modern Unix-like shells for decades.