fg

process managementLinux/Unix
The fg command is one of the most frequently used commands in Linux/Unix-like operating systems. fg Move job to the foreground

Quick Reference

Command Name:

fg

Category:

process management

Platform:

Linux/Unix

Basic Usage:

fg [options] [arguments]

Common Use Cases

    Syntax

    fg [job_spec]

    Options

    Job Specification Description
    %n Job number n
    %str Job whose command begins with str
    %?str Job whose command contains str
    %% Current job
    %+ Current job (same as %%)
    %- Previous job

    Examples

    How to Use These Examples

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

    # Basic Examples Basic
    fg
    Bring the most recently backgrounded job to the foreground.
    fg %1
    Bring job number 1 to the foreground.
    # Advanced Examples Advanced
    fg %emacs Bring the job containing "emacs" in its command to the foreground. fg %- Bring the previous job to the foreground. fg %?txt Bring the job containing "txt" to the foreground.

    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

    The fg command is a shell built-in that brings a job that was suspended or placed in the background back to the foreground, allowing it to receive input from the terminal and display output to the terminal. Key points about fg: 1. Job Control: fg is part of the shell's job control mechanism, which allows you to manage multiple processes within a single terminal session. 2. Relationship with bg and jobs: - bg: Places a job in the background (continues execution without terminal control) - jobs: Lists all current jobs - fg: Brings a background job to the foreground 3. Default Behavior: If no job is specified, fg brings the most recently backgrounded or suspended job to the foreground. 4. Job Specification: Jobs can be specified in various ways using the % prefix, such as by job number, command name, or special symbols like + (current job) or - (previous job). 5. Terminal Control: When a job is brought to the foreground, it gains control of the terminal, meaning it can receive input and send output directly to the terminal. 6. Suspending Jobs: You can suspend a running foreground job by pressing Ctrl+Z, which stops the job and returns control to the shell. The suspended job can then be resumed in the foreground (with fg) or background (with bg). 7. Shell Dependency: The fg command is a shell built-in and its behavior may vary slightly between different shells (bash, zsh, etc.).

    Related Commands

    These commands are frequently used alongside fg 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 fg command works in different scenarios.

    $ fg
    View All Commands