bg

process managementlinux
The bg command is one of the most frequently used commands in Linux/Unix-like operating systems. bg The bg command continues a suspended job in the background. It allows you to resume the execution of jobs that have been suspended (using Ctrl+Z) while returning control of the terminal to you.

Quick Reference

Command Name:

bg

Category:

process management

Platform:

linux

Basic Usage:

bg [options] [arguments]

Common Use Cases

    Syntax

    bg [job_spec...]

    Options

    Option Description
    job_spec Specifies the job to continue in the background. If no job is specified, the most recently suspended job is used.

    Job Specification Formats:

    Format Description Example
    %n Job number n bg %1
    %str Job whose command begins with str bg %find
    %?str Job whose command contains str bg %?log
    %% Current job bg %%
    %+ Current job (same as %%) bg %+
    %- Previous job bg %-

    Examples

    How to Use These Examples

    The examples below show common ways to use the bg command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Suspend a running process with Ctrl+Z, then resume it in background
    sleep 100
    # Press Ctrl+Z to suspend
    bg
    # The sleep command now runs in the background
    # Use job number to specify which job to put in background bg %1
    # Continue the most recently suspended job in the background bg
    # Put multiple jobs in background at once bg %1 %2 %3

    Advanced Examples:

    # Start a command, suspend it, and continue in background with output redirection
    find / -name "*.log" > logs.txt
    # Press Ctrl+Z to suspend
    bg
    # Now find continues in background with output still going to logs.txt
    # List current jobs before using bg jobs bg %2
    # Create a sequence of background processes for i in {1..5}; do echo "Starting job $i" sleep 100 # Press Ctrl+Z after each sleep starts bg done # Use bg with a job specification by command name sleep 200 # Press Ctrl+Z bg %?sleep # Resume a specific job and disown it (to make it immune to hangups) bg %1 disown %1

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Understanding Job Control:

    The bg command is part of the job control facility provided by Unix/Linux shells like bash, zsh, and others. Job control allows you to manage multiple processes within a single terminal session.

    • Job control lets you move processes between foreground and background
    • A foreground process receives input from and sends output to your terminal
    • A background process runs without taking control of your terminal
    • The bg command specifically moves suspended jobs to the background

    Relationship to Other Job Control Commands:

    • jobs: Lists all current jobs
    • fg: Brings a background job to the foreground
    • Ctrl+Z: Suspends the current foreground job
    • kill: Terminates a job
    • disown: Removes a job from the shell's job table
    • wait: Waits for a background job to finish
    • nohup: Runs a command immune to hangups, with output to a non-tty

    Important Considerations:

    • Jobs run in the background still have their standard output and error connected to the terminal by default
    • Background jobs can be terminated if you log out of the session unless you use nohup or disown
    • Some programs cannot be run in the background effectively, especially those requiring user input
    • The shell needs to support job control for bg to work (most modern shells do)
    • Jobs can only be controlled by the shell that started them

    Common Patterns:

    • Start a long-running process → suspend with Ctrl+Z → resume in background with bg
    • Check status of background jobs with jobs command
    • Redirect output to files when running jobs in the background
    • Use disown after bg to make jobs persist after the terminal is closed
    • For new commands, directly background them with command & instead of using bg

    Shell-Specific Differences:

    The bg command might behave slightly differently depending on which shell you're using:

    • In bash, bg is a built-in command
    • In csh and tcsh, job specifications can use % or just the job number
    • Some shells provide additional options for job control

    Related Commands

    These commands are frequently used alongside bg or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the bg command works in different scenarios.

    $ bg
    View All Commands