5

I am learning different types of Ubuntu command and there is a command that is

[command] & – run <command> and send task to background

I can't understand this command. Please tell me about the command in details with an example.

0

4 Answers 4

13

It is simply saying that appending an & after a command will run that command in the background. Example:

sleep 10

This will wait for 10 seconds before returning you to the prompt. Add the & after it and you get the prompt right away.

13

What this extract of (I guess a Bash manual?) wants to tell you is simply that you can run any other terminal command in background by appending a & to its end.

Background means that the command will start running, but then its input gets disconnected from the terminal and you get back to the shell prompt instead, so that you can run other commands while your previous one might still be active.

Commands in background may still produce output, which gets shown in the terminal then. This can get confusing if multiple commands produce output at the same time and get mixed up then.

You can bring the last command you had sent to the background back to the foreground again in order to interact with it by typing the command fg.

0

here & is symbol which makes command run in background

if & is given at the end of the command it executes the command in background in a different subshell and throws the output to terminal as it is completed Ex

$ ls &

[3] 23204
2048  Documents         github        Public           test.js

here 23204 is the PID of the background task

Note:- A background process will not stay alive after the shell session is closed. SIGHUP terminates all running processes.

1
  • 2
    “throws the output to terminal as it is completed” Not really, it is appearing when it comes and can even get messed up with the output of other tasks.
    – Melebius
    Jan 11, 2018 at 16:09
0

As the other answers say, it runs the command in the background, which means that it doesn't wait for that command to finish before letting you issue more commands.

However, I don't think the examples given in the other answers are very useful. For most people, backgrounding is most useful if you're running in a window system. Bring up a terminal window and issue the command

xterm

This will bring up a second terminal window, in which you can enter more commands. Leave that second window open and go back to the first one. You'll see that you can't enter any more commands there, because it's waiting for the command xterm to finish. Close the second window (click its close icon or type exit or press ctrl-D at the prompt) and you'll see that the first window comes back to life.

Now, issue the command

xterm &

Again, this will bring up a new window but, if you go back to the first one, you can issue more commands there, even while the second one is open.

Not the answer you're looking for? Browse other questions tagged .