fg Command with Examples in Linux

This tutorial explains Linux “fg” command, options and its usage with examples.

fg – Runs jobs in the foreground.

DESCRIPTION

Resume job in the foreground, and make it the current job. If job is not present, the shell’s notion of the current job is used. The return value is that of the command placed into the foreground, or failure if run when job control is disabled or, when run with job control enabled, if jobid does not specify a valid job or jobid specifies a job that was started without job control.

Simply put, You can bring a background job to the foreground using fg command. When executed without arguments, it will take the most recent background job to the foreground.

SYNOPSIS

fg [JobID]

jobid
Specifies the job that you want to run in the background.

advertisement
advertisement

The JobID parameter can be a process ID number, or you can use one of the following symbol combinations:

%Number
Refers to a job by the job number.
%String
Refers to a job whose name begins with the specified string.
%?String
Refers to a job whose name contains the specified string.
%+ OR %%
Refers to the current job.
%-
Refers to the previous job.

EXAMPLES

1. Simple Example

Note: Join free Sanfoundry classes at Telegram or Youtube

If you have multiple background ground jobs, and would want to bring a certain job to the foreground, execute jobs command which will show the job id and command.

In the following example, fg %1 will bring the job#1 (i.e download-file.sh) to the foreground.

# jobs
[1]   Running                 bash download-file.sh &
[2]-  Running                 evolution &
[3]+  Done                    nautilus .
 
# fg %1

2. Bring Specific job using jobid to foreground

advertisement

If the output of the jobs -l command shows the following job running in the background:

[1] + 16477 Running Sleep 100 &

use the process ID to run the sleep 100 & command in the foreground by entering:

# fg 16477

The screen displays:

advertisement
sleep

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.