Linux fg Command Examples

The fg command in Linux is a shell builtin used to bring a background or stopped job to the foreground for interaction. It is commonly used with job control in shells like Bash. Below are 10 examples showcasing different uses of the fg command with detailed explanations. These examples assume you are using Bash with job control enabled (default in interactive shells).

1. Resume the Most Recent Background Job

Brings the most recently sent background job to the foreground without specifying a job ID.

# Start a job in the background
tail -f /var/log/syslog &

# List jobs
jobs

# Bring the last job to foreground
fg

Example Output: The tail -f /var/log/syslog command resumes in the foreground, displaying log updates until interrupted (e.g., Ctrl+C).

2. Resume a Specific Job by Job ID

Uses the job ID from the jobs command to bring a specific job to the foreground.

# Start multiple background jobs
vim document.txt &
sleep 120 &

# List jobs (e.g., [1] vim document.txt, [2] sleep 120)
jobs

# Bring job 1 to foreground
fg %1

Example Output: The vim document.txt job resumes in the foreground, opening the Vim editor.

3. Resume a Job by Command Name Prefix

References a job by the starting characters of its command.

# Start a background job
nano notes.txt &

# List jobs
jobs

# Bring the 'nano' job to foreground
fg %nano

Example Output: The nano notes.txt job resumes, allowing you to edit the file in the foreground.

4. Resume a Suspended Job

Brings a job stopped with Ctrl+Z back to the foreground.

# Start an interactive job
gedit file.txt
# Press Ctrl+Z to suspend

# List jobs (e.g., [1] Stopped gedit file.txt)
jobs

# Resume in foreground
fg

Example Output: The gedit file.txt job resumes, showing the graphical text editor.

5. Resume a Background Job in a Multi-Job Scenario

Manages multiple jobs and brings a specific one to the foreground.

# Start several jobs
find /usr -name "*.conf" &
python3 script.py &
watch date &

# List jobs
jobs

# Bring the 'python3' job to foreground
fg %python3

Example Output: The python3 script.py job resumes in the foreground, running the Python script.

6. Resume Previous Job with %-

Uses %- to bring the second-to-last job to the foreground.

# Start jobs
sleep 300 &
top &

# List jobs (e.g., [1] sleep 300, [2] top)
jobs

# Bring the previous job (sleep 300)
fg %-

Example Output: The sleep 300 job resumes in the foreground.

7. Combine fg with bg for Job Management

Shows how fg interacts with bg for job control.

# Start and suspend a job
nano report.txt
# Press Ctrl+Z to suspend

# Send to background
bg %1

# Bring back to foreground
fg %1

Example Output: The nano report.txt job is suspended, moved to the background, then resumed in the foreground for editing.

8. Resume a Job by Partial Command Match

Uses a partial command name to match and resume a job.

# Start a job
grep -r "error" /var/log &

# List jobs
jobs

# Bring job to foreground using partial match
fg %grep

Example Output: The grep job resumes, displaying matching lines in the foreground.

9. Handle Multiple Instances of the Same Command

Manages multiple jobs with the same command name using job IDs.

# Start multiple vim sessions
vim file1.txt &
vim file2.txt &

# List jobs (e.g., [1] vim file1.txt, [2] vim file2.txt)
jobs

# Bring the second vim session to foreground
fg %2

Example Output: The vim file2.txt job resumes in the foreground.

10. Error Handling for Invalid Job

Attempts to resume a non-existent job to demonstrate error handling.

# List jobs (assume no jobs running)
jobs

# Try to bring a non-existent job to foreground
fg %3

Example Output: bash: fg: %3: no such job
Use jobs to verify available job IDs before using fg.

Note: The fg command requires job control, enabled by default in interactive Bash shells. Use jobs to list active jobs and bg to run jobs in the background. For more details, run help fg or man bash. Examples assume an interactive Bash session.