fgBring a job back to the foreground
fg brings a background or stopped job back to the foreground: it gets the terminal again, receives your keystrokes and Ctrl-C, and the shell waits for it. It's how you return to a vim you suspended with Ctrl-Z or check on a job you started with &.
fg [%JOB]While editing a config you need to check a value on the box, then get back to exactly where you were.
fgbash builtin: gives the terminal to the job and resumes it.
fg finds the job in bash's job table, prints its command line, makes its process group the terminal's foreground group with tcsetpgrp, and sends SIGCONT to the group in case it was stopped. bash then blocks in waitpid until the job exits or stops again. From now on Ctrl-C (SIGINT), Ctrl-\ (SIGQUIT) and Ctrl-Z (SIGTSTP) go to the job, and it can read the keyboard. Full-screen programs like vim or less redraw their screen when they get SIGCONT. When the job finishes, bash takes the terminal back and prints a prompt. fg only works in a shell with job control (interactive bash, or a script with set -m); elsewhere it fails with 'no job control'.
(no argument)The current job (+)%NJob number N%-The previous job (the one marked -)%nameJob whose command starts with name%?textJob whose command contains text%N (alone)Typing just %1 at the prompt is shorthand for fg %1Using fg in a script to wait for a background commandScripts run without job control, so fg fails with 'no job control'.
cmd & pid=$!; ...; wait $pidTyping fg in a new SSH session to get back to a job from the old oneThe job belonged to the old shell, which ended (and probably sent it SIGHUP).
Use tmux or screen and reattach with tmux attachAssuming fg restarts the job from the beginningfg continues the same process from where it stopped; it does not run the command again.
Run the command again if you really want a fresh startTry these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 120 &`, bring it to the foreground, then stop it with Ctrl-C.
Open `less /etc/passwd`, suspend it, run `whoami`, then go back to less.
Suspend two programs (less and sleep 600), then bring back less by name.
fgBring a job back to the foreground
fg brings a background or stopped job back to the foreground: it gets the terminal again, receives your keystrokes and Ctrl-C, and the shell waits for it. It's how you return to a vim you suspended with Ctrl-Z or check on a job you started with &.
fg [%JOB]While editing a config you need to check a value on the box, then get back to exactly where you were.
fgbash builtin: gives the terminal to the job and resumes it.
fg finds the job in bash's job table, prints its command line, makes its process group the terminal's foreground group with tcsetpgrp, and sends SIGCONT to the group in case it was stopped. bash then blocks in waitpid until the job exits or stops again. From now on Ctrl-C (SIGINT), Ctrl-\ (SIGQUIT) and Ctrl-Z (SIGTSTP) go to the job, and it can read the keyboard. Full-screen programs like vim or less redraw their screen when they get SIGCONT. When the job finishes, bash takes the terminal back and prints a prompt. fg only works in a shell with job control (interactive bash, or a script with set -m); elsewhere it fails with 'no job control'.
(no argument)The current job (+)%NJob number N%-The previous job (the one marked -)%nameJob whose command starts with name%?textJob whose command contains text%N (alone)Typing just %1 at the prompt is shorthand for fg %1Using fg in a script to wait for a background commandScripts run without job control, so fg fails with 'no job control'.
cmd & pid=$!; ...; wait $pidTyping fg in a new SSH session to get back to a job from the old oneThe job belonged to the old shell, which ended (and probably sent it SIGHUP).
Use tmux or screen and reattach with tmux attachAssuming fg restarts the job from the beginningfg continues the same process from where it stopped; it does not run the command again.
Run the command again if you really want a fresh startTry these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 120 &`, bring it to the foreground, then stop it with Ctrl-C.
Open `less /etc/passwd`, suspend it, run `whoami`, then go back to less.
Suspend two programs (less and sleep 600), then bring back less by name.