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

324,434

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.

Solution 4

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.

Solution 5

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

Share:
324,434

Related videos on Youtube

Community
Author by

Community

Updated on September 18, 2022

Comments

  • Community
    Community over 1 year

    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?

    • Admin
      Admin about 12 years
      I think you may just be writing your desktop file poorly. Can you post (verbatim) the .desktop launcher that you wrote?
    • Admin
      Admin about 12 years
      Also, have you seen this? askubuntu.com/q/34597/24694
    • Admin
      Admin about 12 years
      Its just a launcher where you assign name and command to do its not that big idea
    • Admin
      Admin over 11 years
      Also note, that scripts in linux are not called bash but shell afaik.
    • Admin
      Admin over 8 years
      muru tipped me off to this, worked for me: askubuntu.com/questions/286621/…
    • Admin
      Admin over 5 years
      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 and code commit on GitLab with discussion
  • Admin
    Admin about 12 years
    It still will ask you what to do with run in terminal or display or run ... So its not what i need
  • Admin
    Admin about 12 years
    Broth Ali sure i did it its obvious to do that
  • Admin
    Admin about 12 years
    i did that but what prob is the terminal just twinkles and nothing happens
  • ryanjdillon
    ryanjdillon almost 11 years
    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).
  • whale_steward
    whale_steward about 9 years
    for those who cannot call the app, you need to install it first with sudo apt-get install dconf-editor
  • shrx
    shrx about 9 years
    This works everywhere except on the Desktop. How can I make it work on the Desktop as well? I'm using Xubuntu 14.10.
  • childofsoong
    childofsoong about 9 years
    Is there a way to do this in LXDE (for Lubuntu)?
  • raddevus
    raddevus over 8 years
    This works great under slax unix to -- see slax.org for the really great Unix system you can boot off of a thumb drive.
  • Hendry Tanaka
    Hendry Tanaka about 8 years
    Nice answer!! This answer should be mark as correct right? Haha..
  • Brian Cowan
    Brian Cowan about 8 years
    Works great on RHEL 7.2 as well... Saved me some digging..
  • jcubic
    jcubic over 7 years
    Is there a way to make this work in XFce?
  • Kunok
    Kunok about 7 years
    Only this answer worked on Gnome Ubuntu 17.04 for making desktop file executeable
  • niry
    niry over 6 years
    Now 'Files -> Preferences'. (17.04)
  • codepleb
    codepleb over 6 years
    Sweet! The way to go imho.
  • Sean Bone
    Sean Bone over 6 years
    Major issue: *.cpp files also seem to attempt to execute, however that of course does nothing. It's quite a pain.
  • bad_keypoints
    bad_keypoints about 6 years
    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.
  • Amr Lotfy
    Amr Lotfy over 5 years
    How to see output ?
  • BuvinJ
    BuvinJ about 5 years
    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...
  • DimiDak
    DimiDak about 5 years
    This is for executing file on terminal, not by double clicking it.
  • cipricus
    cipricus over 4 years
    Here on how to achieve the purpose with custom actions in Thunar (without re-enabling that "risky" feature).
  • cipricus
    cipricus over 4 years
    Also here on how to use Zenity and get a question "Edit or Run?"
  • Mr-Programs
    Mr-Programs over 4 years
    get this as untrusted tho
  • muyustan
    muyustan about 4 years
    This method does not allow you to be able to launch by double clicking.
  • gog
    gog over 2 years
    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!