Linux – Why does a background process, started in bash, print stdout to screen

bashdebian-jessielinux

I have trouble understanding my situation and Google was not very helpful so far.

I started a bash background job:

ping google.com &

First I get the process ID, than Bash prints stdout to screen.

user@host:~$ ping google.com &
[1] 14004
user@host:~$ PING google.com (173.194.44.66) 56(84) bytes of data.
64 bytes from ham02s14-in-f2.1e100.net (173.194.44.66): icmp_seq=1 ttl=54 time=26.3 ms
64 bytes from ham02s14-in-f2.1e100.net (173.194.44.66): icmp_seq=2 ttl=54 time=27.4 ms
64 bytes from ham02s14-in-f2.1e100.net (173.194.44.66): icmp_seq=3 ttl=54 time=29.2 ms
...

This contradicts everything I read today. I have a standard Debian Jessie setup running GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu).

Can anyone explain this to me? Thanks.

Best Answer

By default, at least for POSIX compliant systems, running a command in background, with &, only detaches stdin, so you'll be able to run other commands. stdout and stderr are still attached to the parent shell.

If you don't want to see stdout and/or stderr you can just redirect them to a file or /dev/null:

command &>log.txt &                #stdout and stderr to log.txt
command 1>/dev/null &               #stdout to /dev/null, stderr still attached to shell
command 1>output.log 2>error.log &  #stdout to output.log, stderr to error.log
Related Question