Bash: Getting standard program for file type


the background is a shell script to open the .m3u file of a web radio station. Therefore I want to know inside the script, what's the user's program to open such files. At the moment, he has to set the environment variable $PLAYER, but obviously that is not a good way to go.

Alternative: Is there a command that takes a filename and searches itself for an appropriate program to handle that file? Like file, e.g.,

open-file my_playlist.m3u

The script should be portable, it will run at least on Ubuntu, Debian and Windows/Cygwin machines.


Best Solution

This will have to be done differently on each platform. On Mac OS X the "open" command will do what you want.

In Linux it gets murky, since the desktop environment (GNOME or KDE) keeps its own list of applications to run for each file type.

There are two files you can look for in Ubuntu / GNOME that hold this info: ~/.local/share/applications/defaults.list and ~/.local/share/applications/mimeinfo.cache

Someone else hopefully knows how to do this in Windows and can chime in.

Edit: Stealing from the other answers:


xdg-open [filename]


cygstart [filename]

And for completeness, here's a link to a previous question about how to detect which operating system you are running on: Detect OS from bash Script