Linux provides the “&” operator and the “Ctrl-Z” keyboard key for managing the jobs states such as “background,” “foreground,” and “stopped. The “&” operator switches the foreground jobs in the “background” state. On the other hand, the “Ctrl-Z” key places the foreground jobs into the “stopped” state. The definition of both these utilities shows that they are different from each other.
This post provides the key differences between the “&” operator and the “Ctrl-Z” key.
- What is “&” in Bash Linux?
- What is “Ctrl-Z” in Bash Linux?
- Difference Between Ctrl-Z and “&” in Bash Linux
What is “&” in Bash Linux?
The “&” is an ampersand operator in Linux that is used to place the current running process i.e “foreground process” in the background. This is because the shell can’t execute the other command until the previous one completes its execution.
It is useful to execute the time-consuming foreground processes in the background and performs other tasks in the current shell environment.
Example: Place the Command in Background.
The “&” uses at the end of the particular command. In this scenario, the “sample.sh” script is placed in the background having job ID  and process ID “12848” automatically.:
$ ./sample.sh &
Execute the “jobs” command to list down all the background jobs having their two possible states “Running” or “Stopped”:
The output shows that the “sample.sh” script is in the “Running” state in the subshell i.e background.
Resume the Background Job
To resume the background job use the “fg(foreground)” job control command with the particular job ID:
$ fg %1
The “sample.sh” script has completed its execution successfully, showing the “Done” state.
What is “Ctrl+Z” in Bash Linux?
The “Ctrl+Z” is the combination of keyboard keys to stop the foreground process and keep it in the “stopped” state. It can be used at any point of the process/command execution. It shows the “stooped” status at the point where the process is suspended, having a Job ID.
Let’s see its practical implementation with the help of an example.
Example: Stopped the Foreground Process
There is a foreground process, i.e., installation of the “curl” command line tool. Press the “Ctrl+Z” to stop it:
$ sudo apt install curl
The above “apt” command has been stopped.
Run the “jobs” command to verify the stopped process:
The output shows that the “sudo apt install curl” foreground process has been stopped.
Resume the Stooped/Suspended Process
Again, execute the “fg” command without specifying the job Id to resume the recent suspended foreground job:
The output shows that the “apt install curl” command has been resumed.
Difference Between Ctrl-Z and “&” in Bash Linux
The key differences between “Ctrl-Z” and the “&” are stated here:
- Objective: The “&” ampersand operator puts the foreground process in the background subshell whereas the “Ctrl-Z” suspends the foreground process.
- Usage: The “&” operator uses to execute more than one command at a time in the terminal, while the “Ctrl-Z” uses to stop the current running process for the time being.
- Implementation: The “&” operator must be placed at the end of the command. On the other hand, the “Ctrl-Z” key can be pressed at any point in the process.
- States: The “&” operator represents that the process is in the “background” state, and the “Ctrl-Z” specifies the suspended process is in the “Stopped” state.
Bash supports the “Ctrl-Z” key to suspend the foreground jobs i.e., in a “Stopped” state. Whereas the “&(ampersand)” symbol puts the jobs in the background, keeping them in a “Running” state. Apart from their definition, they are also different based on their “objectives”, “usage”, “implementation,” and “states”.
This post has highlighted the key differences between Ctrl-Z and “&” in Bash Linux.