What is the difference between fg, bg, and & in bash?

Answer

& at the end of a command runs it in the background (a background job): long_command &. The shell immediately returns a prompt. bg resumes a suspended (Ctrl+Z) job in the background: bg %1 where %1 is the job number. fg brings a background or suspended job to the foreground: fg %1. jobs lists all current jobs with their numbers and status (running/stopped). Ctrl+Z suspends the foreground process (sends SIGTSTP). Ctrl+C terminates it (sends SIGINT). Background jobs may output to the terminal unexpectedly — redirect their output: cmd > output.log 2>&1 &.