Ubuntu – Launch command in gnome-terminal, then close gnome-terminal without ending executed command

bashcommand linegnome-terminalyad

I have a semi oddball thing I'm tinkering with. I'm looking for a way to make launching MPV (video player) to video links easier. MPV's default behavior is to either launch mpv https://url.of.video in terminal, or drag/drop the URL to the MPV window. I don't find this kosher being a keyboard centric person. Instead I'd like to initiate a popup box, paste the URL, hit enter, and have MPV launch the video to that URL. That way I could tie this command to a keyboard shortcut, making the full process crazy easy. I have this complete, but with one minor papercut — the gnome-terminal window hangs around. It's a simple one liner:

URL=$(yad --entry) && gnome-terminal -- bash -c "mpv $URL"

If I launch that command, I get a popup box thanks to YAD. At this point I paste the URL, hit enter, and in 1-2 seconds an MPV window opens to that video. Awesome. Exactly what I wanted.

The paper cut I mentioned is the fact that gnome-terminal stays running. Not a massive issue, but I'd like to figure out if there's a way for this command above to launch gnome-terminal, call upon MPV, append the variable (URL), and then have gnome-terminal close itself without ending the video stream.

Curious if anybody has any ideas. Thanks much!

Best Answer

Solution to your problem

The right solution here is to omit calling gnome-terminal and bash and even setting an unneeded variable in the first place, all of that is not needed in your case:

mpv -- "$(yad --entry)"

I added quotes to handle the URL correctly, this ensures it can contain e.g. whitespaces and ampersand signs (&) and the command still works. -- tells mpv to stop looking for options and treats whatever you choose to give it as an argument instead. Image the common case where you have a file named --version in the directory you start mpv from and just enter this filename, how can mpv know whether to show you its version string or open the file?

Answer to your question

nohup or disown can be used to detach a process from the shell that started it, see Differences between "<command> & disown" and "nohup <command> & disown".

In your case the correct solution would be:

gnome-terminal -e nohup mpv -- "$(yad --entry)"

However for a much better solution see above.

Related Question