Print XDG Desktop Definition for Application

For an application given by „application name“ or „executable name“, output the corresponding .desktop file, if any:

#!/bin/sh
IFS=":"
xdg_data_dirs=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
search=$1

for i in $xdg_data_dirs ; do
  a="$i/applications"

  [ -d $a ] && for d in "$a"/*.desktop ; do
    grep -q -e "^Name=.*$search" -e "^Exec=.*$search" "$d" && {
      echo "# $d:"
      grep -Ev '^(Comment|GenericName|Keywords|Name\[)' "$d"
    }
  done
done

To try this code out, save it to /usr/local/bin/xdg-desktop-search, make it executable and test it, for example, as follows:

xdg-desktop-search gnome-terminal