Bash – Opening a file with program in background from shell

background-processbashfiles

The command to launch a program, say evince, from bash shell in background is

evince &

But what if I want to open some file with evince in background? Nor evince myfile.pdf & neither evince & myfile.pdf work as expected: the first outputs something like [2] 5356
but in the next line the shell returns locked; the second opens an empty istance of evince.

So, what syntax can I use?

Best Answer

Sometimes, when you start a program, it prints out some message. If you start it in background, the program may lock up until you bring it back to the foreground so that it can display its message. The solution is to redirect stdout and stderr so that the program can continue running in the background. One way to do this is:

evince myfile.pdf >~/evince.errs 2>&1 &

The above creates a file in your home directory with whatever message evince wanted to display.

If you are convinced that evince's messages are unimportant, you can discard them without creating a file:

evince myfile.pdf >/dev/null 2>&1 &

After running either of the above commands, the shell should produce a message like [1] 1234 and then a shell prompt should appear. As Graeme suggests, if the shell prompt does not appear, try pressing enter again.