14

I need to be able to execute an executable shell script (sh) with a double click. I set the executable flag on the permissions for the file, and yet when I double click on it, it opens in a text editor. I do not have any options in my UI under the files properties menu to use a custom command or anything. It only lists various applications which are installed. I just want it to execute, nothing more. How can I accomplish this?

2
  • You possibly have to log off and on again for some file managers to notice that change in executable flag as described here
    – Cadoiz
    Oct 20, 2021 at 11:39
  • You can use xdg-mime query filetype shell_script.sh to find the mime type of the script. Afterwards, use xdg-mime query default mime_type to see the current default application associated with the mime type, which is called upon xdg-open invocation. Then you can use xdg-mime default default_application.desktop mime_type to set the default application to be used. Find details in this answer!
    – Cadoiz
    Oct 20, 2021 at 14:32

3 Answers 3

16

To run your script by double clicking on its icon, you will need to create a .desktop file for it:

[Desktop Entry]
Name=My script
Comment=Test hello world script
Exec=/home/user/yourscript.sh
Icon=/home/user/youricon.png
Terminal=false
Type=Application

Save the above as a file on your Desktop with a .desktop extension. Change /home/user/yourscript.sh and /home/user/youricon.png to the paths of your script and whichever icon you want it to have respectively and then you'll be able to launch by double clicking it.

4
  • I think in Centos 7 shuld be Exec[$e]='/home/user/yourscript.sh'
    – taliezin
    Mar 12, 2015 at 17:43
  • The above solution works fine for me without adding the Exec[$e] on Centos 7. Perhaps a later revision changed what is required. Mar 31, 2017 at 17:03
  • 1
    @Cadoiz thanks for the edit, but desktop files ignore the shebang line. They are not scripts, so the shebang is completely irrelevant and unnecessary. As for %u, that is only needed if you want to pass parameters to the script, so isn't relevant here. See specifications.freedesktop.org/desktop-entry-spec/….
    – terdon
    Oct 20, 2021 at 15:01
  • Thank you! The reader of this Q/A can consider my answer here, where I provide a script to automatically generate these .desktop-files.
    – Cadoiz
    Oct 20, 2021 at 15:52
10

Its actually very simple to do that. Just go to your Nautilus files preferences and click on the 'Behavior' tab. Under 'Executable Text Files' check 'Ask each time' or 'Run executable text files when they are opened'. Please look at the screenshot for reference.

Files Preferences

1
  • This is actually the correct answer. You don't need a .desktop file.
    – user64141
    May 25, 2019 at 3:51
2

This involves using dconf-editor (sudo yum install dconf-editor):

Open dconf-editor, and navigate to the /org/gnome/nautilus/preferences menu.

Under "executable-text-activation", change "Use default value" to OFF, and "Custom value" to 'launch'.

You can then exit dconf-editor.

At this point, double-clicking on an icon which is a script, or a symbolic link to a script, will execute it, rather than opening up the file with gedit, which is what my system was doing.

So after changing the above preference, all I had to do is:

$ cd $HOME/Desktop

$ ln -s $HOME/bin/myscript.sh MyScript

and that's that.

(My system is GNOME Version 3.1.2 running on CentOs 7)

You have to log-out and log-in again before the double click on the desktop icon works.

You must log in to answer this question.

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