I've noticed that calling xdg-open
from a subshell will reliably block until the launched process is closed. I suspect there may be a reason for this, but I'm not sure as to why. For example, launching Nautilus doesn't block when calling xdg-open
directly from the command line:
xdg-open ~/dir ; echo foo # doesn't block
but invoking xdg-open from a subshell will reliably block the terminal
var=$(xdg-open ~/dir ; echo foo) # blocks
{ xdg-open ~/dir ; echo foo ; } | cat # blocks.
My understanding is that xdg-open
detaches the launched process from the shell session so that it's no longer a subprocess. I'd therefore expect this to be different to e.g. invoking sleep 1 &
in a subshell for which it seems reasonable that the terminating subshell will block until all subprocess have completed, i.e.
var=$(sleep 1 & echo foo) # also blocks, but understandable.
But if xdg-open
is detaching the process, what's causing the subshell to wait?
In what may (?) be a partial answer, I've noticed that running
{ xdg-open <file> ; ps ; } | cat
shows that depending on program launched by <file>
, those that block are also the ones that keep the tty as their controlling terminal. That begs the question why this happens, why this happens only in a subshell and ultimately what's a good way to a launch desktop process from the terminal that will fully and reliably detach from it?
Edit: fix syntax on bash
.
bash
, note that subshells are started with(...)
. – Eduardo Trápani Dec 12 '20 at 21:48zsh
, but I observe the same withbash
. Note the use of{...}
was to simplify the example - the output is piped tocat
so my understanding is it is still invoked in a subshell. You get the same behavior inbash
if you wrap in a function from which you pipe the output. – wardw Dec 12 '20 at 21:57xdg-open
will end up calling the appropriate application, so your results may vary, they may even depend on whether there's already an open window for that application or not... – Eduardo Trápani Dec 12 '20 at 22:01xdg-open
from a subshell without blocking the shell that invokesxdg-open
. Whenxdg-open
is invoked outside of a subshell the shell doesn't block - I would like to do this reliably in both cases, and better understand why there's a difference. – wardw Dec 12 '20 at 22:15xdg-open ... &
– Eduardo Trápani Dec 12 '20 at 22:21