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?
- 807,993
- 194
- 1,674
- 2,175
- 243
- 1
- 2
- 7
-
You possibly have to log off and on again for some file managers to notice that change in executable flag [as described here](https://unix.stackexchange.com/a/655803/318461) – Cadoiz Oct 20 '21 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](https://unix.stackexchange.com/a/643473/318461)! – Cadoiz Oct 20 '21 at 14:32
3 Answers
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.
- 234,489
- 66
- 447
- 667
-
-
The above solution works fine for me without adding the Exec[$e] on Centos 7. Perhaps a later revision changed what is required. – Heather92065 Mar 31 '17 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 https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables. – terdon Oct 20 '21 at 15:01
-
Thank you! The reader of this Q/A can consider [my answer here](https://unix.stackexchange.com/a/674052/318461), where I provide a script to automatically generate these `.desktop`-files. – Cadoiz Oct 20 '21 at 15:52
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.
- 411,918
- 54
- 1,065
- 1,164
- 243
- 2
- 8
-
This is actually the correct answer. You don't need a .desktop file. – user64141 May 25 '19 at 03:51
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.
- 21
- 1
