13

On macOS in the default Terminal running bash, I can type:

open -a Firefox http://www.wikipedia.org

to open a URL with Firefox, or another browser. It also works to launch a program, e.g. open -a maps, and to open a file in the default program, e.g. open file.pdf.

How can I do the same on Ubuntu in the default GNOME Terminal also running bash? I know that I can open a file or URL with xdg-open /path/to/file, and I can open the default browser with sensible-browser. Does Ubuntu have a general command to open files, applications, or files in specific applications?

I am running bash on both machines, 3.2.57(1)-release on macOS and 4.4.19(1)-release on Ubuntu.

  • I've never used a Mac, so can you tell me why you'd use open -a Firefox http://www.wikipedia.org? Can't you do firefox http://www.wikipedia.org? It seems like a useless feature of open, when opening programs is the very point of bash. – JoL Oct 4 '18 at 15:26
  • 1
    Bash on macOS works differently, e.g. your command gives the error bash: firefox: command not found. Same error with Firefox as program name. – mmorin Oct 4 '18 at 16:36
  • 3
    bash works exactly the same; it's Firefox that is installed differently than it is on Ubuntu. On macOS, you could, for instance, run /Applications/Firefox.app/Contents/MacOS/firefox-bin from the command line. Firefox.app is a special kind of folder that contains not just the executable, but files that in Linux might be scattered about in various /usr/bin/, /usr/lib, /etc, etc. directories. The Finder knows how to run the actual application when you, for instance, double-click on Firefox.app. open is a way to "run" the special folder from the command line. – chepner Oct 4 '18 at 18:53
  • 4
    It's not that bash works differently: firefox is installed as an OS X application (Firefox.app) and is not on the regular PATH. Another reason to use open -a is that it properly backgrounds the application it launches, instead of waiting on it until it exits. – alexis Oct 4 '18 at 18:54
  • askubuntu.com/a/15356 & – Dev Oct 4 '18 at 20:00
10

You can specifically use your desired program's name (if it's able to be used as a command line tool).

For example, urls:

firefox duckduckgo.com
chromium-browser askubuntu.com

PDF:

evince foo.pdf
okular bar.pdf

Images:

gpicview foo.png
feh bar.jpeg

Texts:

gedit foo.txt
mousepad /etc/config
leafpad bar.xml

Video/Music:

mpv  foo.mp3
vlc  bar.mp4

If you want the program to be run detached from the terminal then this is the way that I prefer doing it:

nohup program args &

For example:

nohup firefox askubuntu.com &

Remember that you can always redirect the outputs as usual, e.g. :

nohup firefox duckduckgo.com &> /dev/null &
  • 2
    eog picture.jpg for images too if you have gnome-desktop installed :) – Videonauth Oct 4 '18 at 11:19
  • 1
    @Videonauth Yeah, or any other kind of tools ;) – Ravexina Oct 4 '18 at 11:20
  • 2
    @BaptisteCandellier I don't get the point of calling open to call another program. Why not just call the other program directly? What usefulness does open add? This answer reflects the natural thing to do to open specific applications. – JoL Oct 4 '18 at 15:35
  • 1
    @BaptisteCandellier I agree, and open on macOS works both for opening files and folders in the default application and in specific applications. It seems the original bash went on a different path with xdg-open for files and the application names for the rest. – mmorin Oct 4 '18 at 17:00
  • 2
    Another thing that is different about open in MacOS is that it launches the application detached from the console it's run from, i.e. if the console closes, the app will stay on. On Ubuntu one needs to use the nohup command to get the same behaviour. (since prefixing each command with nohup and redirecting output for each command you run gets tiring, it may make more sense to define a custom shortcut) – undercat Oct 5 '18 at 4:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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