157

How can I make a bash script executable by double clicking just like .exe files in Windows? I tried creating a launcher and assigning the script to it, but there are two consequences:

  1. the terminal twinkles, disappears, and nothing is done.
  2. you must specify to run in terminal in order to work.

I have a script that installs tomcat on an offline PC, including all dependencies of tomcat in the script. I need to make the script work on double clicking like windows since most who use the script will not be familiar with Ubuntu.

Forget the above explanation. I want to make a script that can be run by double-clicking on it, without using the terminal. Anybody knows how?

  • I think you may just be writing your desktop file poorly. Can you post (**verbatim**) the .desktop launcher that you wrote? – Jjed May 18 '12 at 15:25
  • Also, have you seen this? http://askubuntu.com/q/34597/24694 – Jjed May 18 '12 at 15:27
  • Its just a launcher where you assign name and command to do its not that big idea –  May 19 '12 at 06:38
  • Also note, that scripts in linux are not called _bash_ but _shell_ afaik. – cauon Oct 05 '12 at 08:58
  • 3
    muru tipped me off to this, worked for me: http://askubuntu.com/questions/286621/how-do-i-run-executable-scripts-in-nautilus – draoi Nov 16 '15 at 23:56
  • 1
    FYI for future visitors: Nautilus, the default file manager on Ubuntu, apparently will be removing the ability to run executables in newer releases in 2019 due to vulnerabilities. [Article on the topic](https://www.omgubuntu.co.uk/2018/05/nautilus-remove-ability-launch-binaries-apps) and [code commit on GitLab with discussion](https://gitlab.gnome.org/GNOME/nautilus/commit/3a22ed5b8e3bbc1c59ff3069ee79755168754916) – Sergiy Kolodyazhnyy Jan 12 '19 at 02:53

12 Answers12

163

I tested this and it worked fine. Ensure your script begins with the correct shebang, e.g. #!/usr/bin/env bash .

Then follow these steps:

  • Hit Alt+F2 to run a command.
  • Type dconf-editor and hit Enter.
  • In dconfg-editor go to: org ➤ gnome ➤ nautilus ➤ preferences
  • Click on executable-text-activation and from drop down menu select:

    launch: to launch scripts as programs.
    OR
    ask: to ask what to do via a dialog.

    screenshot

  • Close dconf-editor. Thats it!

Alternative way, using terminal: Run:

gsettings set org.gnome.nautilus.preferences executable-text-activation 'launch'

Note: The file should be executable. You may also need to change permission of file. Right click and set "Allow executing file as program"

screenshot

Source.

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
stevenmc
  • 1,784
  • 1
  • 11
  • 10
  • This worked great. Just a note, the "drop down menu" is reached by clicking on the value listed to the right of `executable-text-activation` (default value is `display`). – ryanjdillon Aug 14 '13 at 10:50
  • 10
    for those who cannot call the app, you need to install it first with `sudo apt-get install dconf-editor` – whale_steward Mar 14 '15 at 06:35
  • 1
    This works everywhere except on the Desktop. How can I make it work on the Desktop as well? I'm using Xubuntu 14.10. – shrx Mar 28 '15 at 17:17
  • Is there a way to do this in LXDE (for Lubuntu)? – childofsoong May 02 '15 at 20:05
  • This works great under slax unix to -- see http://slax.org for the really great Unix system you can boot off of a thumb drive. – raddevus Dec 05 '15 at 22:42
  • Nice answer!! This answer should be mark as correct right? Haha.. – Hendry Tanaka Feb 22 '16 at 04:15
  • Works great on RHEL 7.2 as well... Saved me some digging.. – Brian Cowan Apr 14 '16 at 19:51
  • Is there a way to make this work in XFce? – jcubic Jan 14 '17 at 20:31
  • Major issue: *.cpp files also seem to attempt to execute, however that of course does nothing. It's quite a pain. – Sean Bone Jan 09 '18 at 10:48
  • Alt+F2 is supposed to launch the terminal right? Wasted a few minutes there, trying to launch dconf-editor in tty of Ctrl+Alt+F2, as Alt+F2 did nothing in my system. Just had to launch the terminal, launch dconf-editor there. Worked. – bad_keypoints May 04 '18 at 07:23
100

On Nautilus (AKA Files)

  1. Go to Files (Edit on Unity (17.04 or below)) > Preferences > Behavior tab

    screenshot

  2. Change the settings for executable text file.

    screenshot

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
Anon
  • 1,001
  • 1
  • 7
  • 2
19

I think creating a *.desktop file is missing here:

$ cat shortcut-for-my-script.desktop
[Desktop Entry]
Type=Application
Terminal=true
Name=Click-Script
Icon=utilities-terminal
Exec=gnome-terminal -e "bash -c './script.sh;$SHELL'"
Categories=Application;

If you create more than one *.desktop file for one script you are able to create multiple configurations by passing different parameters, which is a very comfortable and user-friendly way.

h0ch5tr4355
  • 664
  • 9
  • 20
  • 1
    This the best answer for Ubuntu. Note that you may not (or probably do not?) need to specify `Terminal=True` and `Exec=gnome-terminal` unless you are literally creating a shortcut for a script that you want to see the output of within a terminal window... Also note that you can point `Icon` to whatever custom png you might want... – BuvinJ Mar 20 '19 at 12:52
  • get this as untrusted tho – Mr-Programs Dec 08 '19 at 21:07
  • 1
    I was launching a script that launches other applications, and them crashed after a couple of seconds, being hit by SIGHUP. Using `bash -c "./script.sh; $SHELL"` like stated here solved the problem. Great! – gog Nov 12 '21 at 11:53
10

For Ubuntu 20.04:

  1. Open Files (Nautilus is called Files now), click on the hamburger menu and select Preferences.

enter image description here

  1. Go to the Behaviour tab and select what's suitable from the Executable Text Files section

enter image description here

Willi Mentzel
  • 638
  • 1
  • 7
  • 20
10

In Ubuntu and all Unix based OSes, afaik, nothing is set as executable by default. There are two ways of doing this.

  1. Right click on the script or file you want to execute. Go to Properties then to the Permissions Tab. Click the check box that says Execute.

  2. Open a Terminal cd into the directory where the file is found. Type chmod ugo+x filename. This will set the file to execute.

Now your users can double click to their hearts content.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
SomKit
  • 430
  • 2
  • 8
8

If you're using XFce then you can make script executed in Thunar by executing this command:

xfconf-query --channel thunar --property /misc-exec-shell-scripts-by-default \
             --create --type bool --set true

or by using gui xfce4-settings-editor and creating the same properties.

Run xfce4-settings-editor, then enter:

  • /misc-exec-shell-scripts-by-default

  • Boolean

  • TRUE

picture of xfce4-settings-editor

cipricus
  • 3,102
  • 1
  • 25
  • 70
jcubic
  • 916
  • 5
  • 16
  • 29
  • [Here](https://askubuntu.com/a/1170255/925128) on how to achieve the purpose with custom actions in Thunar (without re-enabling that "risky" feature). – cipricus Sep 02 '19 at 12:38
  • Also [here](https://askubuntu.com/a/876581/925128) on how to use Zenity and get a question "Edit or Run?" – cipricus Sep 02 '19 at 13:14
2

You have to check 3 points :

sangorys
  • 329
  • 3
  • 11
1

As a complement to the answer about Thunar: that answer would re-enable a previous feature of Thunar to execute scripts on double click. But that feature was considered a security risk.

To avoid that, maybe the most elegant solution is to get a pop-up window with a question like "What do you want to with that file: Run or edit?" (similar to what you have in other file managers like PCManFM and Nemo) when clicking that file - by using Zenity: here.


An alternative would be custom actions, perhaps the best feature Thunar has.

The command that I use for running an executable text file is sh -c %f, for a custom action called "Run/Execute script".

enter image description here

The appearance conditions are simply "text":

enter image description here

A similar command can be used for running in terminal a such script ("Run in terminal"), while keeping the Xfce4 terminal open:

xfce4-terminal -H -x %f

enter image description here

Optionally, and maybe preferably, in order to hide such options for all text files except the executable ones, a way is to restrict appearance use to .sh files (or .py files if the case, etc), under "Appearance conditions":

enter image description here

-- before renaming the executable files accordingly.

cipricus
  • 3,102
  • 1
  • 25
  • 70
1

When you create a launch in Desktop to a SHEL script or Whatever, in command line, don't forget to signal the script, to be executed in background.

The script will die when he finished is job.

  • 1
    i did that but what prob is the terminal just twinkles and nothing happens –  May 19 '12 at 06:36
1

Based on umask defualt value, you haven't execute permissions for files by default and must change mod to gain execute permissions, something like this:

chmod u+x filename
Ali Dehghani
  • 496
  • 6
  • 9
-1

OS: Linux Mint (I'm using 18.3)

I struggled for ages with this, trying to find out how you can click (or double-click) an executable item and get it to run, without further questions. I looked at all the stuff under Nemo Edit --> Preferences. The answer is actually very simple, and set up for you... I don't know whether this applies solely to Mint.

On the desktop, right-click and choose "+ Create a new launcher here". One of the simplest imaginable dialogs then follows, where you have to choose your executable file, and whether you also want to create a new item in the Start menu, i.e. to run using exclusively keyboard.

What this actually creates is a simple executable text file, e.g.

[Desktop Entry]
Comment=
Terminal=true
Name=MyScript
Exec=/media/mike/D_drive/My Documents/sysadmin/MyScript.sh
Type=Application

There's no requirement for this file to have ".desktop" in its name.

This file can be used elsewhere than on the desktop: move/copy it into another directory and it will also run with a double-click in Nemo (or single click if Nemo is so configured).

mike rodent
  • 168
  • 7
-1

Specially for Android Studio You can follow my original answer here:

To Create Desktop Launcher:

  • Click on Create Desktop Entry.. from Tools menu.

enter image description here

  • It will be prompt for password, enter password of your current login.

  • Finally it will be display notification in Android Studio screen like:

enter image description here

  • You can see in Applications => Programming => Android Studio, and you may see on desktop also after restarting system.

Thank you. :)

Pratik Butani
  • 155
  • 1
  • 12