To run a process in the background in bash is fairly easy.
$ echo "Hello I'm a background task" &
[1] 2076
Hello I'm a background task
[1]+ Done echo "Hello I'm a background task"
However the output is verbose. On the first line is printed the job id and process id of the background task, then we have the output of the command, finally we have the job id, its status and the command which triggered the job.
Is there a way to suppress the output of running a background task such that the output looks exactly as it would without the ampersand at the end? I.e:
$ echo "Hello I'm a background task" &
Hello I'm a background task
The reason I ask is that I want to run a background process as part of a tab-completion command so the output of that command must be uninterrupted to make any sense.
The subshell solution works, but I also wanted to be able to wait on the background jobs (and not have the "Done" message at the end).
$!
from a subshell is not "waitable" in the current interactive shell. The only solution that worked for me was to use my own wait function, which is very simple:Easy enough.
Building on the above answer, if you need to allow stderr to come through from the command:
Try:
And you have hidden both the output and the PID. Note that you can still retrieve the PID from $REPLY
Not related to completion, but you could supress that output by putting the call in a subshell:
Based on this answer, I came up with the more concise and correct:
Sorry for the response to an old post, but I figure this is useful to others, and it's the first response on Google.
I was having an issue with this method (subshells) and using 'wait'. However, as I was running it inside a function, I was able to do this:
And when I run it:
And there's a delay of 5 seconds before I get my prompt back.