Parameter expansion: is it possible to extract an internal substring?

Thanks, I will look at it tomorrow.

Thatā€™s a UUOC!!1!
:scream_cat:

But then you go on and break my heart even more with:

head ... | cut ... | awk ...

Why not throw in a couple of seds and maybe a sort | uniq, while youā€™re at it? Itā€™s plumbing, not scriptingā€¦

That was a quick and dirty lineā€¦ :wink:
And didnā€™t even cover all possible cases either.

But I urge you to make a better one. Can you make it a one liner? :sweat_smile:

Why should it be a one-liner? Multiple lines are easier to read :wink:

This solution is pure Bash, no external processes, no plumbing:

FILE="./test.desktop"

while read -r LINE; do
  if [[ "$LINE" =~ ^[[:space:]]*Exec=(.*)$ ]]; then
    EXEC_COMMAND="${BASH_REMATCH[1]}"
    break
  fi
done < "$FILE"

echo "$EXEC_COMMAND"
1 Like

Nice script. :+1:

This is the output concerning all current desktop files in /usr/share/applications. It shows that some more work is required, and it is not a generally simple task:

adie
akm
appdoc
assistant
atril %U
/usr/bin/avahi-discover
bluefish %F
/usr/bin/bssh
/usr/bin/bvnc
dconf-editor
calculator
cmake-gui %f
/opt/vscodium-bin/bin/codium --no-sandbox --unity-launch %F
/opt/vscodium-bin/bin/codium --open-url %U
tilix
drawing %U
ControlPanel
/usr/bin/xdg-open http://localhost:631/
designer %F
sh -c "if [ -n \\"\\$*\\" ]; then exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" \\"\\$@\\"; else exec emacsclient --alternate-editor= --create-frame; fi" placeholder %F
sh -c "exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" --eval \\\\(message-mailto\\\\ \\\\\\"%u\\\\\\"\\\\)"
emacs %F
emacs -f message-mailto %u
engrampa %U
eos-apps-info
/usr/bin/eos-log-tool
eos-quickstart
eos-update-notifier-configure
feh --start-at %u
/usr/lib/firefox/firefox %u
/usr/lib/firefox-developer-edition/firefox --class="firefoxdeveloperedition" %u
/usr/bin/firewall-config
galculator
/usr/lib/gcr-prompter
/usr/bin/gcr-viewer
geany %F
gkbd-keyboard-display
gnome-disk-image-mounter %U
gnome-disks --restore-disk-image %U
kde-geo-uri-handler --coordinate-template "https://www.google.com/maps/@<LAT>,<LON>,<Z>" --query-template "https://www.google.com/maps/search/<Q>" --fallback "https://www.google.com/maps/" %u
/usr/bin/gparted %f
/usr/sbin/gtk-lshw
gufw
htop
celluloid %U
diffuse -s %F
kcmshell5 kcm_trash
kdiskmark
ktelnetservice5 %u
libfm-pref-apps
libreoffice --base %U
libreoffice --calc %U
libreoffice --draw %U
libreoffice --impress %U
libreoffice --math %U
libreoffice %U
libreoffice --writer %U
libreoffice %U
linguist %F
llpp %f
lstopo
lxappearance
lxshortcut -i %f
mate-calc
mpv --player-operation-mode=pseudo-gui -- %U
nitrogen
nm-applet
nm-connection-editor
kde-geo-uri-handler --coordinate-template "https://www.openstreetmap.org/#map=<Z>/<LAT>/<LON>" --query-template "https://www.openstreetmap.org/search?query=<Q>" --fallback "https://www.openstreetmap.org" %u
avidemux3_qt5 %f
gnome-chess %U
gnome-disks
eog %U
file-roller %U
gedit %U
meld %F
iagno
gnome-screenshot --interactive
gnome-terminal
totem %U
inkscape %F
dolphin %u
kaffeine %U
kate -b %U
kcalc
kcalc
kmines -qwindowtitle %c
konsole
UserFeedbackConsole
kwalletd5
ksnip
parole %F
ristretto %U
xfce4-panel --add=launcher %u
xfce4-panel --preferences
PathFinder
pavucontrol
pcmanfm %U
pcmanfm --desktop-pref
photoflare %f
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
qdbusviewer
qpdfview --unique %F
qt5ct
/usr/bin/qtcreator %F
qterminal
qterminal --drop
qv4l2
qvidcap
kde-geo-uri-handler --coordinate-template "https://www.qwant.com/maps/#map=<Z>/<LAT>/<LON>" --query-template "https://www.qwant.com/maps/?q=<Q>&client=opensearch" --fallback "https://www.qwant.com/maps/" %u
/usr/bin/reflector-simple
simple-scan
solaar
sol
soundconverter %U
stickburn --fg
stoken-gui
stoken-gui --small
supertux2
system-config-printer
terminator
thunar --bulk-rename %F
thunar %F
thunar-settings
thunar-volman-settings
/usr/lib/thunderbird/thunderbird %u
transmission-gtk %U
unison-gtk2
UpdateInTerminal --nt
gksu -w UserManager
uxterm
viewnior %F
VirtualBox %U
/usr/bin/vlc --started-from-file %U
sh /usr/share/endeavouros/scripts/welcome --once
kde-geo-uri-handler --coordinate-template "https://wheelmap.org/?lat=<LAT>&lon=<LON>" --query-template "https://wheelmap.org/search?q=<Q>" --fallback "https://wheelmap.org" %u
xdvi %f
xed %U
xfburn
xfce4-about
xfce4-accessibility-settings
xfce4-appfinder
xfce4-color-settings
exo-open --launch FileManager %u
exo-open --launch MailReader %u
xfce4-mime-settings
xfce4-notifyd-config
xfce4-power-manager-settings
xfce4-appfinder --collapsed
xfce4-screensaver-preferences
xfce4-screenshooter
xfce4-sensors
xfce4-session-logout
xfce4-settings-editor
xfce4-taskmanager
xfce4-terminal
exo-open --launch TerminalEmulator
xfce4-terminal --preferences
exo-open --launch WebBrowser %u
xfdesktop-settings
xfce4-display-settings
xfce4-keyboard-settings
xfce4-mouse-settings
xfce4-session-settings
xfce4-settings-manager
xfce4-appearance-settings
xfwm4-settings
xfwm4-tweaks-settings
xfwm4-workspace-settings
xreader %U
xterm
yad-icon-browser
yad-settings

How do I use your solution? What is FILE="./test.desktop"?

If I change "./test.desktop" to "/usr/share/applications/xed.desktop", the result is xed %U.

(Despite the title of the thread, Iā€™m trying to make my own desktop launcher instead of relying on something like

#!/bin/bash
rofi \
   -show drun \
   -width 30 \
   -drun-match-fields "exec"

Currently, Iā€™m going with

#!/bin/bash

rg --hidden Exec= /usr/share/applications/*.desktop $HOME/.local/share/applications/*.desktop \
| sed 's/\s%[uUfF]//' \
| cut -d"=" -f2 \
| sort -u \
| fzf -e --cycle --reverse \
| xargs -I{} setsid -f bash -c "{}"

Lots of ā€œplumbingā€ :blush:

I see nothing wrong with that output. Sometimes you want the arguments to be passed to applications.

If you want to just get rid of everything after the first space, just modify the regular expression line into:

if [[ "$LINE" =~ ^[[:space:]]*Exec=([^ ]*) ]]; then

Itā€™s actually a very simple task. :wink:

@eosbtw absolutely disgusting.

1 Like

Now the output is:

adie
akm
appdoc
appdoc
atril %U
atril %U
atril %U
atril %U
atril %U
dconf-editor
calculator
cmake-gui %f
cmake-gui %f
cmake-gui %f
tilix
drawing %U
ControlPanel
ControlPanel
ControlPanel
ControlPanel
ControlPanel
ControlPanel
ControlPanel
engrampa %U
engrampa %U
engrampa %U
engrampa %U
engrampa %U
engrampa %U
engrampa %U
engrampa %U
engrampa %U
galculator
galculator
galculator
geany %F
geany %F
geany %F
geany %F
geany %F
geany %F
geany %F
gufw
htop
celluloid %U
celluloid %U
celluloid %U
celluloid %U
celluloid %U
celluloid %U
celluloid %U
libreoffice --calc %U
libreoffice --draw %U
libreoffice --draw %U
libreoffice --math %U
libreoffice %U
libreoffice --writer %U
libreoffice %U
libreoffice %U
llpp %f
llpp %f
lxappearance
lxappearance
mate-calc
mate-calc
nitrogen
nm-applet
nm-connection-editor
nm-connection-editor
avidemux3_qt5 %f
avidemux3_qt5 %f
avidemux3_qt5 %f
eog %U
file-roller %U
gedit %U
meld %F
iagno
iagno
gnome-terminal
totem %U
totem %U
dolphin %u
kaffeine %U
kate -b %U
kcalc
kcalc
kcalc
kcalc
kcalc
kwalletd5
kwalletd5
parole %F
parole %F
xfce4-panel --add=launcher %u
xfce4-panel --add=launcher %u
PathFinder
pavucontrol
pcmanfm %U
pcmanfm %U
photoflare %f
photoflare %f
photoflare %f
qpdfview --unique %F
qt5ct
qt5ct
qterminal
qterminal --drop
qv4l2
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
qvidcap
terminator
thunar --bulk-rename %F
thunar %F
thunar %F
thunar %F
thunar %F
thunar %F
thunar %F
UpdateInTerminal --nt
UpdateInTerminal --nt
uxterm
viewnior %F
VirtualBox %U
VirtualBox %U
VirtualBox %U
VirtualBox %U
xdvi %f
xed %U
xfburn
xfce4-about
xfce4-about
xfce4-appfinder
xfce4-appfinder
exo-open --launch FileManager %u
exo-open --launch MailReader %u
exo-open --launch MailReader %u
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-notifyd-config
xfce4-terminal
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
exo-open --launch TerminalEmulator
xreader %U
xterm
xterm
xterm

EDIT: sorry, a copying typo from my part. The output is:

adie
akm
appdoc
a
atril %U
/u
bluefi
/u
/u
dconf-editor
calculator
cmake-gui %f
/opt/v
/opt/v
tilix
drawing %U
ControlPanel
/u
de


emac
emac
engrampa %U
eo
/u
eo
eo
feh --
/u
/u
/u
galculator
/u
/u
geany %F
gkbd-keyboard-di
gnome-di
gnome-di
kde-geo-uri-handler --coordinate-template "http
/u
/u
gufw
htop
celluloid %U
diffu
kcm
kdi
ktelnet
libfm-pref-app
libreoffice --ba
libreoffice --calc %U
libreoffice --draw %U
libreoffice --impre
libreoffice --math %U
libreoffice %U
libreoffice --writer %U
libreoffice %U
lingui
llpp %f
l
lxappearance
lx
mate-calc
mpv --player-operation-mode=p
nitrogen
nm-applet
nm-connection-editor
kde-geo-uri-handler --coordinate-template "http
avidemux3_qt5 %f
gnome-che
gnome-di
eog %U
file-roller %U
gedit %U
meld %F
iagno
gnome-
gnome-terminal
totem %U
ink
dolphin %u
kaffeine %U
kate -b %U
kcalc
kcalc
kmine
kon
U
kwalletd5
k
parole %F
ri
xfce4-panel --add=launcher %u
xfce4-panel --preference
PathFinder
pavucontrol
pcmanfm %U
pcmanfm --de
photoflare %f
/u
qdbu
qpdfview --unique %F
qt5ct
/u
qterminal
qterminal --drop
qv4l2
qvidcap
kde-geo-uri-handler --coordinate-template "http
/u









terminator
thunar --bulk-rename %F
thunar %F
thunar-
thunar-volman-
/u
tran
uni
UpdateInTerminal --nt
gk
uxterm
viewnior %F
VirtualBox %U
/u

kde-geo-uri-handler --coordinate-template "http
xdvi %f
xed %U
xfburn
xfce4-about
xfce4-acce
xfce4-appfinder
xfce4-color-
exo-open --launch FileManager %u
exo-open --launch MailReader %u
xfce4-mime-
xfce4-notifyd-config
xfce4-power-manager-
xfce4-appfinder --collap
xfce4-
xfce4-
xfce4-
xfce4-
xfce4-
xfce4-ta
xfce4-terminal
exo-open --launch TerminalEmulator
xfce4-terminal --preference
exo-open --launch WebBrow
xfde
xfce4-di
xfce4-keyboard-
xfce4-mou
xfce4-
xfce4-
xfce4-appearance-
xfwm4-
xfwm4-tweak
xfwm4-work
xreader %U
xterm
yad-icon-brow
yad-

Sorry, I just wrote it without testing it, and I forgot Bash does not have \s. So, instead of [^\s], it should be [^ ], like this:

if [[ "$LINE" =~ ^[[:space:]]*Exec=([^ ]*) ]]; then

Now it works correctly. You should have figured that out yourself :rofl:

Just loop through all the files in the targeted directory, for example, like this:

#!/bin/bash

for FILE in /usr/share/applications/*; do
  if [ -f "$FILE" ]; then
    while read -r LINE; do
      if [[ "$LINE" =~ ^[[:space:]]*Exec=([^ ]*) ]]; then
        EXEC_COMMAND="${BASH_REMATCH[1]}"
        break
      fi
    done < "$FILE"
  fi
  echo "$EXEC_COMMAND"
done
1 Like

Yeah, that problem is now fixed ā€¦ :wink:

adie
akm
appdoc
assistant
atril
/usr/bin/avahi-discover
bluefish
/usr/bin/bssh
/usr/bin/bvnc
dconf-editor
calculator
cmake-gui
/opt/vscodium-bin/bin/codium
/opt/vscodium-bin/bin/codium
tilix
drawing
ControlPanel
/usr/bin/xdg-open
designer
sh
sh
emacs
emacs
engrampa
eos-apps-info
/usr/bin/eos-log-tool
eos-quickstart
eos-update-notifier-configure
feh
/usr/lib/firefox/firefox
/usr/lib/firefox-developer-edition/firefox
/usr/bin/firewall-config
galculator
/usr/lib/gcr-prompter
/usr/bin/gcr-viewer
geany
gkbd-keyboard-display
gnome-disk-image-mounter
gnome-disks
kde-geo-uri-handler
/usr/bin/gparted
/usr/sbin/gtk-lshw
gufw
htop
celluloid
diffuse
kcmshell5
kdiskmark
ktelnetservice5
libfm-pref-apps
libreoffice
libreoffice
libreoffice
libreoffice
libreoffice
libreoffice
libreoffice
libreoffice
linguist
llpp
lstopo
lxappearance
lxshortcut
mate-calc
mpv
nitrogen
nm-applet
nm-connection-editor
kde-geo-uri-handler
avidemux3_qt5
gnome-chess
gnome-disks
eog
file-roller
gedit
meld
iagno
gnome-screenshot
gnome-terminal
totem
inkscape
dolphin
kaffeine
kate
kcalc
kcalc
kmines
konsole
UserFeedbackConsole
kwalletd5
ksnip
parole
ristretto
xfce4-panel
xfce4-panel
PathFinder
pavucontrol
pcmanfm
pcmanfm
photoflare
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
qdbusviewer
qpdfview
qt5ct
/usr/bin/qtcreator
qterminal
qterminal
qv4l2
qvidcap
kde-geo-uri-handler
/usr/bin/reflector-simple
simple-scan
solaar
sol
soundconverter
stickburn
stoken-gui
stoken-gui
supertux2
system-config-printer
terminator
thunar
thunar
thunar-settings
thunar-volman-settings
/usr/lib/thunderbird/thunderbird
transmission-gtk
unison-gtk2
UpdateInTerminal
gksu
uxterm
viewnior
VirtualBox
/usr/bin/vlc
sh
kde-geo-uri-handler
xdvi
xed
xfburn
xfce4-about
xfce4-accessibility-settings
xfce4-appfinder
xfce4-color-settings
exo-open
exo-open
xfce4-mime-settings
xfce4-notifyd-config
xfce4-power-manager-settings
xfce4-appfinder
xfce4-screensaver-preferences
xfce4-screenshooter
xfce4-sensors
xfce4-session-logout
xfce4-settings-editor
xfce4-taskmanager
xfce4-terminal
exo-open
xfce4-terminal
exo-open
xfdesktop-settings
xfce4-display-settings
xfce4-keyboard-settings
xfce4-mouse-settings
xfce4-session-settings
xfce4-settings-manager
xfce4-appearance-settings
xfwm4-settings
xfwm4-tweaks-settings
xfwm4-workspace-settings
xreader
xterm
yad-icon-browser
yad-settings

Looks good to me. What else do you want? :rofl:

Did you look at the output? It is likely not what the OP wanted.

Can you be more specific?

There are same commands with some different programs.

So? I really do not understand what do you meanā€¦

I would stick to my original regexp, if you want different commands for different programs.

Even that might not be suitable for a generic launcher.

But this is the current output, sorted and it shows the files too:

adie                                     /usr/share/applications/adie.desktop               
akm                                      /usr/share/applications/akm.desktop                
appdoc                                   /usr/share/applications/appdoc.desktop             
assistant                                /usr/share/applications/assistant.desktop          
atril                                    /usr/share/applications/atril.desktop              
avidemux3_qt5                            /usr/share/applications/org.avidemux.Avidemux.desktop 
bluefish                                 /usr/share/applications/bluefish.desktop           
calculator                               /usr/share/applications/calculator.desktop         
celluloid                                /usr/share/applications/io.github.celluloid_player.Celluloid.desktop 
cmake-gui                                /usr/share/applications/cmake-gui.desktop          
ControlPanel                             /usr/share/applications/ControlPanel.desktop       
dconf-editor                             /usr/share/applications/ca.desrt.dconf-editor.desktop 
designer                                 /usr/share/applications/designer.desktop           
diffuse                                  /usr/share/applications/io.github.mightycreak.Diffuse.desktop 
dolphin                                  /usr/share/applications/org.kde.dolphin.desktop    
drawing                                  /usr/share/applications/com.github.maoschanz.drawing.desktop 
emacs                                    /usr/share/applications/emacs.desktop              
emacs                                    /usr/share/applications/emacs-mail.desktop         
engrampa                                 /usr/share/applications/engrampa.desktop           
eog                                      /usr/share/applications/org.gnome.eog.desktop      
eos-apps-info                            /usr/share/applications/eos-apps-info.desktop      
eos-quickstart                           /usr/share/applications/eos-quickstart.desktop     
eos-update-notifier-configure            /usr/share/applications/eos-update-notifier-configure.desktop 
exo-open                                 /usr/share/applications/xfce4-file-manager.desktop 
exo-open                                 /usr/share/applications/xfce4-mail-reader.desktop  
exo-open                                 /usr/share/applications/xfce4-terminal-emulator.desktop 
exo-open                                 /usr/share/applications/xfce4-web-browser.desktop  
feh                                      /usr/share/applications/feh.desktop                
file-roller                              /usr/share/applications/org.gnome.FileRoller.desktop 
galculator                               /usr/share/applications/galculator.desktop         
geany                                    /usr/share/applications/geany.desktop              
gedit                                    /usr/share/applications/org.gnome.gedit.desktop    
gkbd-keyboard-display                    /usr/share/applications/gkbd-keyboard-display.desktop 
gksu                                     /usr/share/applications/UserManager.desktop        
gnome-chess                              /usr/share/applications/org.gnome.Chess.desktop    
gnome-disk-image-mounter                 /usr/share/applications/gnome-disk-image-mounter.desktop 
gnome-disks                              /usr/share/applications/gnome-disk-image-writer.desktop 
gnome-disks                              /usr/share/applications/org.gnome.DiskUtility.desktop 
gnome-screenshot                         /usr/share/applications/org.gnome.Screenshot.desktop 
gnome-terminal                           /usr/share/applications/org.gnome.Terminal.desktop 
gufw                                     /usr/share/applications/gufw.desktop               
htop                                     /usr/share/applications/htop.desktop               
iagno                                    /usr/share/applications/org.gnome.Reversi.desktop  
inkscape                                 /usr/share/applications/org.inkscape.Inkscape.desktop 
kaffeine                                 /usr/share/applications/org.kde.kaffeine.desktop   
kate                                     /usr/share/applications/org.kde.kate.desktop       
kcalc                                    /usr/share/applications/org.kde.kcalc.desktop      
kcalc                                    /usr/share/applications/org.kde.kded5.desktop      
kcmshell5                                /usr/share/applications/kcm_trash.desktop          
kde-geo-uri-handler                      /usr/share/applications/google-maps-geo-handler.desktop 
kde-geo-uri-handler                      /usr/share/applications/openstreetmap-geo-handler.desktop 
kde-geo-uri-handler                      /usr/share/applications/qwant-maps-geo-handler.desktop 
kde-geo-uri-handler                      /usr/share/applications/wheelmap-geo-handler.desktop 
kdiskmark                                /usr/share/applications/kdiskmark.desktop          
kmines                                   /usr/share/applications/org.kde.kmines.desktop     
konsole                                  /usr/share/applications/org.kde.konsole.desktop    
ksnip                                    /usr/share/applications/org.ksnip.ksnip.desktop    
ktelnetservice5                          /usr/share/applications/ktelnetservice5.desktop    
kwalletd5                                /usr/share/applications/org.kde.kwalletd5.desktop  
libfm-pref-apps                          /usr/share/applications/libfm-pref-apps.desktop    
libreoffice                              /usr/share/applications/libreoffice-base.desktop   
libreoffice                              /usr/share/applications/libreoffice-calc.desktop   
libreoffice                              /usr/share/applications/libreoffice-draw.desktop   
libreoffice                              /usr/share/applications/libreoffice-impress.desktop 
libreoffice                              /usr/share/applications/libreoffice-math.desktop   
libreoffice                              /usr/share/applications/libreoffice-startcenter.desktop 
libreoffice                              /usr/share/applications/libreoffice-writer.desktop 
libreoffice                              /usr/share/applications/libreoffice-xsltfilter.desktop 
linguist                                 /usr/share/applications/linguist.desktop           
llpp                                     /usr/share/applications/llpp.desktop               
lstopo                                   /usr/share/applications/lstopo.desktop             
lxappearance                             /usr/share/applications/lxappearance.desktop       
lxshortcut                               /usr/share/applications/lxshortcut.desktop         
mate-calc                                /usr/share/applications/mate-calc.desktop          
meld                                     /usr/share/applications/org.gnome.meld.desktop     
mpv                                      /usr/share/applications/mpv.desktop                
nitrogen                                 /usr/share/applications/nitrogen.desktop           
nm-applet                                /usr/share/applications/nm-applet.desktop          
nm-connection-editor                     /usr/share/applications/nm-connection-editor.desktop 
/opt/vscodium-bin/bin/codium             /usr/share/applications/codium.desktop             
/opt/vscodium-bin/bin/codium             /usr/share/applications/codium-uri-handler.desktop 
parole                                   /usr/share/applications/org.xfce.Parole.desktop    
PathFinder                               /usr/share/applications/PathFinder.desktop         
pavucontrol                              /usr/share/applications/pavucontrol.desktop        
pcmanfm                                  /usr/share/applications/pcmanfm.desktop            
pcmanfm                                  /usr/share/applications/pcmanfm-desktop-pref.desktop 
photoflare                               /usr/share/applications/photoflare.desktop         
qdbusviewer                              /usr/share/applications/qdbusviewer.desktop        
qpdfview                                 /usr/share/applications/qpdfview.desktop           
qt5ct                                    /usr/share/applications/qt5ct.desktop              
qterminal                                /usr/share/applications/qterminal.desktop          
qterminal                                /usr/share/applications/qterminal-drop.desktop     
qv4l2                                    /usr/share/applications/qv4l2.desktop              
qvidcap                                  /usr/share/applications/qvidcap.desktop            
ristretto                                /usr/share/applications/org.xfce.ristretto.desktop 
sh                                       /usr/share/applications/emacsclient.desktop        
sh                                       /usr/share/applications/emacsclient-mail.desktop   
sh                                       /usr/share/applications/welcome.desktop            
simple-scan                              /usr/share/applications/simple-scan.desktop        
solaar                                   /usr/share/applications/solaar.desktop             
sol                                      /usr/share/applications/sol.desktop                
soundconverter                           /usr/share/applications/soundconverter.desktop     
stickburn                                /usr/share/applications/stickburn.desktop          
stoken-gui                               /usr/share/applications/stoken-gui.desktop         
stoken-gui                               /usr/share/applications/stoken-gui-small.desktop   
supertux2                                /usr/share/applications/supertux2.desktop          
system-config-printer                    /usr/share/applications/system-config-printer.desktop 
terminator                               /usr/share/applications/terminator.desktop         
thunar-settings                          /usr/share/applications/thunar-settings.desktop    
thunar                                   /usr/share/applications/thunar-bulk-rename.desktop 
thunar                                   /usr/share/applications/thunar.desktop             
thunar-volman-settings                   /usr/share/applications/thunar-volman-settings.desktop 
tilix                                    /usr/share/applications/com.gexperts.Tilix.desktop 
totem                                    /usr/share/applications/org.gnome.Totem.desktop    
transmission-gtk                         /usr/share/applications/transmission-gtk.desktop   
unison-gtk2                              /usr/share/applications/unison.desktop             
UpdateInTerminal                         /usr/share/applications/UpdateInTerminal.desktop   
UserFeedbackConsole                      /usr/share/applications/org.kde.kuserfeedback-console.desktop 
/usr/bin/avahi-discover                  /usr/share/applications/avahi-discover.desktop     
/usr/bin/bssh                            /usr/share/applications/bssh.desktop               
/usr/bin/bvnc                            /usr/share/applications/bvnc.desktop               
/usr/bin/eos-log-tool                    /usr/share/applications/eos-log-tool.desktop       
/usr/bin/firewall-config                 /usr/share/applications/firewall-config.desktop    
/usr/bin/gcr-viewer                      /usr/share/applications/gcr-viewer.desktop         
/usr/bin/gparted                         /usr/share/applications/gparted.desktop            
/usr/bin/qtcreator                       /usr/share/applications/qtcreator.desktop          
/usr/bin/reflector-simple                /usr/share/applications/reflector-simple.desktop   
/usr/bin/vlc                             /usr/share/applications/vlc.desktop                
/usr/bin/xdg-open                        /usr/share/applications/cups.desktop               
/usr/lib/firefox-developer-edition/firefox /usr/share/applications/firefox-developer-edition.desktop 
/usr/lib/firefox/firefox                 /usr/share/applications/firefox.desktop            
/usr/lib/gcr-prompter                    /usr/share/applications/gcr-prompter.desktop       
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 /usr/share/applications/polkit-gnome-authentication-agent-1.desktop 
/usr/lib/thunderbird/thunderbird         /usr/share/applications/thunderbird.desktop        
/usr/sbin/gtk-lshw                       /usr/share/applications/gtk-lshw.desktop           
uxterm                                   /usr/share/applications/uxterm.desktop             
viewnior                                 /usr/share/applications/viewnior.desktop           
VirtualBox                               /usr/share/applications/virtualbox.desktop         
xdvi                                     /usr/share/applications/xdvi.desktop               
xed                                      /usr/share/applications/xed.desktop                
xfburn                                   /usr/share/applications/xfburn.desktop             
xfce4-about                              /usr/share/applications/xfce4-about.desktop        
xfce4-accessibility-settings             /usr/share/applications/xfce4-accessibility-settings.desktop 
xfce4-appearance-settings                /usr/share/applications/xfce-ui-settings.desktop   
xfce4-appfinder                          /usr/share/applications/xfce4-appfinder.desktop    
xfce4-appfinder                          /usr/share/applications/xfce4-run.desktop          
xfce4-color-settings                     /usr/share/applications/xfce4-color-settings.desktop 
xfce4-display-settings                   /usr/share/applications/xfce-display-settings.desktop 
xfce4-keyboard-settings                  /usr/share/applications/xfce-keyboard-settings.desktop 
xfce4-mime-settings                      /usr/share/applications/xfce4-mime-settings.desktop 
xfce4-mouse-settings                     /usr/share/applications/xfce-mouse-settings.desktop 
xfce4-notifyd-config                     /usr/share/applications/xfce4-notifyd-config.desktop 
xfce4-panel                              /usr/share/applications/panel-desktop-handler.desktop 
xfce4-panel                              /usr/share/applications/panel-preferences.desktop  
xfce4-power-manager-settings             /usr/share/applications/xfce4-power-manager-settings.desktop 
xfce4-screensaver-preferences            /usr/share/applications/xfce4-screensaver-preferences.desktop 
xfce4-screenshooter                      /usr/share/applications/xfce4-screenshooter.desktop 
xfce4-sensors                            /usr/share/applications/xfce4-sensors.desktop      
xfce4-session-logout                     /usr/share/applications/xfce4-session-logout.desktop 
xfce4-session-settings                   /usr/share/applications/xfce-session-settings.desktop 
xfce4-settings-editor                    /usr/share/applications/xfce4-settings-editor.desktop 
xfce4-settings-manager                   /usr/share/applications/xfce-settings-manager.desktop 
xfce4-taskmanager                        /usr/share/applications/xfce4-taskmanager.desktop  
xfce4-terminal                           /usr/share/applications/xfce4-terminal.desktop     
xfce4-terminal                           /usr/share/applications/xfce4-terminal-settings.desktop 
xfdesktop-settings                       /usr/share/applications/xfce-backdrop-settings.desktop 
xfwm4-settings                           /usr/share/applications/xfce-wm-settings.desktop   
xfwm4-tweaks-settings                    /usr/share/applications/xfce-wmtweaks-settings.desktop 
xfwm4-workspace-settings                 /usr/share/applications/xfce-workspaces-settings.desktop 
xreader                                  /usr/share/applications/xreader.desktop            
xterm                                    /usr/share/applications/xterm.desktop              
yad-icon-browser                         /usr/share/applications/yad-icon-browser.desktop   
yad-settings                             /usr/share/applications/yad-settings.desktop       

All in all, your script is great. The problem simply isnā€™t described clearly enough.

Yes! Iā€™m truly sorry about that!

My initial question was about trying to use parameter expansion to extract the executable from the path of a .desktop file.

However, the more experienced people here pointed out the limitations of looking at the path rather than the actual Exec= line.

And, yes, my aim was/is twofold:

  • to make my own ā€œterminal-centricā€ launcher for those GUI applications for which I donā€™t already have convenient keyboard shortcuts
  • to learn something new

Kresimirā€™s contribution is beyond my comprehension though I will, over the weeks to come, try to become acquainted with ~= and BASH_REMATCH, etc.

For now, I have xterm -e fzf-desktop-launcher.sh (contents below) bound to Ctrl+Super+Z:

  • it looks for .desktop files in ~/.local/share/applications and /usr/share/applications
  • it excludes .desktop files with ā€œTerminal=trueā€ and ā€œNoDisplay=trueā€
  • it excludes .desktop files with Exec= lines having %f, %F, %u, and %U as these function, AFAICT, in the context of file managers only
  • cut leaves just the executable
  • sort -u removes duplicate entries
  • fzf -e enables selecting the desired entry by typing a few letters
  • and the last line executes the selection and closes the (temporary) xterm window
#!/bin/bash

list=$(grep -Ei -L "(terminal=true|nodisplay=true|%u|%U|%f|%F)" /usr/share/applications/*.desktop $HOME/.local/share/applications/*.desktop)

grep Exec= $list \
| cut -d "=" -f2 \
| sort -u \
| fzf -e --cycle --reverse \
| xargs -I{} setsid -f bash -c "{}"

General ā€œreverse engineeringā€ of desktop files and creating a generic launcher from them is (I think) next to impossible, or at least very challenging. After all, desktop files are kind of approximations of the intended usage of each.

So (I think) a reasonable approach could be to limit to some subset (defined by you) of all your desktop files and make them work with your script. And if you install new apps, check that they work with your launcher.

With reference to if [[ "$LINE" =~ ^[[:space:]]*Exec=([^ ]*) ]]; then,

  • what is the reason for ^[[:space:]]*Exec? Why not just ^Exec?
  • [^ ] eliminates lines like qdbus org.kde.Spectacle ...
  • Also, in some .desktop files, such as that for Firefox, there are multiple Exec= lines. Again, org.kde.spectacle.desktop is such a case. The present code seems to return just one hit per file whereas this link lists six.

All the same, thereā€™s a lot packed into the code. Trying to understand it will keep me gainfully occupied for quite some time :sweat_smile: