fg Command ExamplesThe 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).
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.