Just to be explicit, Ignacio is correct here in that .desktop files should not be directly executed. It is possible (as you discovered), but unwise.
On another note, do not use xdg-open. It might just happen to work if there is a correctly associated mime-type, but this is not reliable.
You should use gtk-launch. It is used as follows:
gtk-launch APPLICATION [URI...]
gtk-launch app-name.desktop
gtk-launch app-name
Here is the man entry:
NAME
gtk-launch - Launch an application
SYNOPSIS
gtk-launch [APPLICATION] [URI...]
DESCRIPTION
gtk-launch launches an application using the given name. The
application is started with proper startup notification on a default
display, unless specified otherwise.
gtk-launch takes at least one argument, the name of the application to
launch. The name should match application desktop file name, as
residing in /usr/share/application, with or without the '.desktop'
suffix.
If called with more than one argument, the rest of them besides the
application name are considered URI locations and are passed as
arguments to the launched application.
Please note that gtk-launch requires the .desktop file to be installed (i.e. located in /usr/share/applications or $HOME/.local/share/applications).
So to get around this, we can use a hackish little bash function that temporarily installs the desired .desktop file before launching it. The "correct" way to install a .desktop file is via desktop-file-install but I'm going to ignore that.
launch(){
(
# where you want to install the launcher to
appdir=$HOME/.local/share/applications
# the template used to install the launcher
template=launcher-XXXXXX.desktop
# ensure $1 has a .desktop extension, exists, is a normal file, is readable, has nonzero size
# optionally use desktop-file-validate for stricter checking
# if ! desktop-file-validate "$1" 2>/dev/null; then
if [[ ! ( $1 = *.desktop && -f $1 && -r $1 && -s $1 ) ]]; then
echo "ERROR: you have not supplied valid .desktop file" >&2
exit 1
fi
# ensure the temporary launcher is deleted upon exit
trap 'rm "$launcherfile" 2>/dev/null' EXIT
launcherfile=$(mktemp -p "$appdir" "$template")
launchername=${launcherfile##*/}
if cp "$1" "$launcherfile" 2>/dev/null; then
gtk-launch "$launchername" "${@:2}"
else
echo "ERROR: failed to copy launcher to applications directory" >&2
exit 1
fi
exit 0
)
}
You can use it like so (and also pass along additional arguments or URIs if you want):
launch ./path/to/shortcut.desktop
Alternatively, I wrote an answer here that outlines all the ways to launch .desktop files. It offers some alternatives to gtk-launch that may help.
execdesktopusing your system package manager (dpkg? RPM?emerge? etc) the script should probably be in/usr/local/bin, not in/usr/bin.