Exec=gnome-terminal -- bash -c 'script="%u";if [[ -e "$script" ]] ; then "$script" ; else exec bash ; fi' line in GNOME desktop file runs scrips, but only w/out spaces in the name. How to run script with any name?

Full description of "what I want" and what I've tried:

I want to open scripts in editor by default from Nemo (file manager AFAIK based on Nautilus) but still be able to open in terminal as another option in Nemo to open these files with.

Web search finds https://askubuntu.com/questions/286621/how-do-i-run-executable-scripts-in-nautilus. Not new for me, but when I set option in file manager to "view", there is no option to run in "open with", I have to open terminal separately and enter script name.

I've opened /usr/share/applications/org.gnome.Terminal.desktop, it runs Exec=gnome-terminal --window. I've found https://askubuntu.com/questions/974756/how-can-i-open-a-extra-console-and-run-a-program-in-it-with-one-command and changed line Exec=gnome-terminal --window with sample line Exec=gnome-terminal -- bash -c 'script="%u";if [[ -e "$script" ]] ; then "$script" ; else exec bash ; fi', it works for scripts w/out spaces in names, but with scripts that contain spaces it does not result in expected script output. No big deal maybe, but I want better.

Meaning of e.g. %u confirmed here: https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html.

1 Answer 1


The freedesktop page you linked clearly says

Field codes must not be used inside a quoted argument, the result of field code expansion inside a quoted argument is undefined.

I would pass the URL as an argument to bash:

gnome-terminal -- bash -c 'script="$1"; if [[ -e "$script" ]] ; then "$script" ; else exec bash ; fi' bash %u
  • Thanks, man bash explains the solution, searched for parameters, found more about -c. Commented Dec 3, 2022 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .