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:
- the terminal twinkles, disappears, and nothing is done.
- 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 tested this and it worked fine. Ensure your script begins with the correct shebang, e.g.
#!/usr/bin/env bash
.Then follow these steps:
dconf-editor
and hit Enter.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.
Close dconf-editor. Thats it!
Alternative way, using terminal: Run:
Note: The file should be executable. You may also need to change permission of file. Right click and set "Allow executing file as program"
Source.
On Nautilus (AKA Files)
Go to Files (Edit on Unity (17.04 or below)) > Preferences > Behavior tab
Change the settings for executable text file.
I think creating a *.desktop file is missing here:
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.
In Ubuntu and all Unix based OSes, afaik, nothing is set as executable by default. There are two ways of doing this.
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.
Open a Terminal
cd
into the directory where the file is found. Typechmod ugo+x filename
. This will set the file to execute.Now your users can double click to their hearts content.
For Ubuntu 20.04:
If you're using XFce then you can make script executed in Thunar by executing this command:
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
You have to check 3 points :
The file system must allow execution :
mount
in a consolenoexec
flagnoexec
flag by editing/etc/fstab
and remount the modified system (eg. a reboot works)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.
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: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".The appearance conditions are simply "text":
A similar command can be used for running in terminal a such script ("Run in terminal"), while keeping the Xfce4 terminal open:
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":-- before renaming the executable files accordingly.