How to execute a script just by double clicking like .EXE files in Windows?

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?


Solution 1:

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.

Solution 2:

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

Solution 3:

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.