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
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
Brings the most recent background or suspended job to foreground
Bring specific job to foreground
Brings job number 1 to the foreground
Bring job by command name
Brings the job whose command starts with "vim" to foreground
Bring job containing string
Brings the job whose command contains "editor" to foreground
Bring previous job to foreground
Brings the previous job (second most recent) to foreground
Complete workflow example
Complete example of suspending and resuming a text editor
Interactive session management
Managing multiple interactive programs
Development workflow
Typical development environment job management
Resume suspended process
Resuming accidentally suspended processes
Job Control Workflow
- Start processes: Run commands normally or with
& - Suspend: Press
Ctrl+Zto suspend foreground jobs - Check jobs: Use
jobsto see all jobs and their status - Background: Use
bgto resume jobs in background - Foreground: Use
fgto bring jobs to foreground - Terminate: Use
kill %nto 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