Ubuntu: How to escape spaces in .desktop files Exec line


I want to make a .desktop file like described here.

[Desktop Entry]  Name=Sublime Text 2  GenericName=Sublime Text 2  Comment=Edit text files  Exec=/home/user/opt/sublime/Sublime Text 2/sublime_text %U  

However, running that from Nautilus's context menu using Open with this gives me

Could not find '/home/user/opt/sublime/Sublime'

So I tried

Exec="/home/user/opt/sublime/Sublime Text 2/sublime_text" %U  

and got

Text ended before matching quote was found for ". (The text was '"/home/user/opt/sublime/Sublime')

What is the correct way to escape spaces in the Exec line of .desktop files?


Precede each space by a backslash:

[Desktop Entry]  Name=Sublime Text 2  GenericName=Sublime Text 2  Comment=Edit text files  Exec=/home/user/opt/sublime/Sublime\ Text\ 2/sublime_text %U  


I was having exactly the same problem! After trying various escaping/quoting patterns, I decided the simplest solution was to have a symbolic link to sublime_text on my $PATH (or you could use a command-line alias).

[Desktop Entry]  Version=1.0  Type=Application  Terminal=false  Icon[C]=/usr/share/Sublime Text 2/Icon/256x256/sublime_text.png  Name[C]=Sublime Text 2  Exec=Sublime-Text-2 %U  Comment[C]=Text Editor  Name=Sublime Text 2  Comment=Text Editor  Icon=/usr/share/Sublime Text 2/Icon/256x256/sublime_text.png  X-Desktop-File-Install-Version=0.21  


Have you tried using ' ' quotes instead of " " quotes? I have a customised .desktop file with spaces in the Exec line, and mine works with ' ' quotes.


I've got the same problem when following installation instructions from (http://monkeyhacks.com/post/how-to-install-sublime-text-2-on-ubuntu-14-04) site.. So my solution was:

  1. Remove/delete the soft link called "sublime" from /usr/bin

  2. Remove all whitespaces of "Sublime Text 2" folder by renaming it to "SublimeText2"

  3. Re Create a soft link in /usr/bin/:

    sudo ln -s /opt/SublimeText2/sublime_text /usr/bin/sublime  
  4. Make the .desktop file including:

    Exec=/opt/SublimeText2/sublime_text %U  Icon=/opt/SublimeText2/Icon/48x48/sublime_text.png  


According to the desktop entry specification ASCII space may be represented by the \s escape sequence (for string and localestring values).


I ran into the same problem using GNOME nautilus 3.4.2 in Ubuntu 12.04. I found this alternative workaround in a bug report for the same issue in Moblin UI Framework which I prefer to the one mentioned where you need to add every single folder to the $PATH.

The workaround is simply:
"Create a symbolic link in a path that does not have spaces, and point the EXEC field to that link."


.desktop files do not handle quotes, spaces, and escaped characters the same way that shells do. Probably, the best option would be to call a shell in your exec line to get the expected behavior, like so:

[Desktop Entry]  Name=Sublime Text 2  GenericName=Sublime Text 2  Comment=Edit text files  Exec=sh -c "/home/user/opt/sublime/Sublime Text 2/sublime_text" %U  

For more information, please see man sh. This also avoids having to create and maintain symbolic links, as you will with the other answers.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »