Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm starting to experiment with Crunchbang (which is based on Debian, and uses terminator) as a web development environment, and one of the things I am struggling with is the behaviour of xdg-open. I come from an OSX background, so forgive me if this question comes off as dense.

I would like to be able to open a url with xdg-open http://www.google.com and then continue to use the same terminal window to work (it's how open functions in OSX). Right now, using xdg-open occupies the current tab/session until I close browser window, or manually end things with ctrl + c. I'd much prefer it start a new process, that way I can open up a URL, refer to data on the page, and use it in the same tab/window without needing to open an additional one.

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

Strange, it works like that out of the box on my Debian. Try running it in the background:

xdg-open http://www.google.com &

You can make this into a function by adding these lines to your ~/.bashrc file:

function open () {
    xdg-open $1 &
}

You can then simply run open http://www.google.com and it will run in the background.

share|improve this answer
    
This works great. I was hoping to have a simple alias to to open, (i.e. open='xdg-open') is there a way to get the functionality of xdg-open <url> & without using a shell function? –  Nick Tomlin May 7 '13 at 19:27
    
@NickTomlin Not as far as I know, no, but what have you got against functions? As you can see in my updated answer it is almost as simple as an alias. –  terdon May 7 '13 at 20:07
    
Not a particular bias, I was hoping to avoid a function since I am trying to use (and reuse) the same alias for both Mac OS and *nix flavors. –  Nick Tomlin May 7 '13 at 20:20
    
No reason why you couldn't. Functions depend on the shell, as long as you use bash in all systems in question it should work perfectly well. –  terdon May 7 '13 at 20:23
add comment

If you want to detach the process from the current shell rather than starting it as a background job with xdg-open http://www.google.com &, I like the detach utility:

detach xdg-open http://www.google.com

One could create an alias for this. I like detach over nohup as closes stdin stdout and stderr by default so its invocation is cleaner.

share|improve this answer
    
Alas, detach does not seem to exist in my distro. nohup launches the process, but still occupies the terminal window. –  Nick Tomlin May 7 '13 at 19:25
add comment

xdg-open waits for the program to finish. This is by design. If the program is a text mode program, it has to stay in the foreground in the terminal. Even if the program is a GUI one, this behavior is useful in case xdg-open is used from a script and the script wants to perform something after the file has been edited (e.g. send the new version somewhere or otherwise make something with the new version).

If you don't want to wait, run xdg-open in the background. You can run any shell command in the background by putting an ampersand at the end.

xdg-open http://www.google.com &

With some programs, xdg-open returns immediately. What happens is actually that the program that xdg-open invokes returns immediately. This typically happens with GUI programs that open all files in a single instance: when you start them a second time, they send a message the running instance to tell it to open the file, and exit immediately.

share|improve this answer
add comment

Try this:

DISPLAY=:0.0; xdg-open '<url>'

I use this technique to en-queue magnet:// URLs into my Bit Torrent client Vuze.

ssh someserver "DISPLAY=:0.0; xdg-open 'http://www.google.com/'"

The single quotes help to protect the contents of the URLs so that the shell doesn't attempt to interpret them.

share|improve this answer
    
This still keeps the process running in the current terminal window. Let me know if I need to clarify my question. –  Nick Tomlin May 7 '13 at 19:29
    
Yeah, if you want it to go background immediately then just put it to the background with a ampersand "&". Isn't that @terdon's answer said to do? –  slm May 7 '13 at 19:37
    
The issue here isn't xdg-open, see @Gilles answer, he explains why xdg-open is being held up, it's the GUI you're sending the URL to which is causing xdg-open to wait. –  slm May 7 '13 at 19:47
    
What browser are you sending the URL to? Is there a dialog box or anything else being popped when you send the URLs w/ xdg-open? Seems like several of the answerers here have indicated that the normal behavior is that control is returned to the terminal after some period of time. –  slm May 7 '13 at 19:52
    
thanks for the clarification. I've reviewed the answers and I understand this better now. The browser is iceweasal. –  Nick Tomlin May 7 '13 at 19:55
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.