pnkjmndhl
pnkjmndhl

Reputation: 585

How do I run a script on Linux just by double clicking it

I have a script written (which are basically the commands for the terminal for Ubuntu) on a file. Yes, the code starts with

#!/bin/bash

How do I run this script just by double clicking it? It can be run using the terminal but I want to make it more accessible through my desktop.

I was just trying to mimic a *.bat file on windows. A *.bat file on windows contains series of scripts operable on command prompt and executable just by double clicking on it.

Upvotes: 12

Views: 62558

Answers (5)

fortea
fortea

Reputation: 369

An alternative way, mostly for developers, is to create self-contained desktop launchers. The idea is to use a launcher that includes a tag under which there is the script; the launcher executes a command that:

  1. put the script into a temporary file
  2. run the script which deletes itself

Of course, the script should delete itself, for this reason, it's a good practice to start the script with rm $0. Note that if the script needs to read the script itself, you will need to use rm $0 somewhere else...

You can use the following example stolen from my project:

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Launch Assistance
Comment=A simple app to setup remote assistance
Exec=ttt=/tmp/$(date +"%s%N").sh; tail -n+$(awk '/^#__SCRIPT__#/ {print NR + 1; exit 0; }' %k) %k > $ttt ; sh $ttt;
Icon=computer
Terminal=true
Type=Application
Categories=Network;Internet;Utilities;Administration;Settings;

#__SCRIPT__#
rm "$0"
# Put here the script
# note that if the script needs to read $0, you will have to edit it

Upvotes: 3

Gulzar
Gulzar

Reputation: 27896

  1. Open nautilus
  2. Behavior tab
  3. Executable Text files -> Ask each time
  4. Profit

Source. Tested on Ubuntu 20.04.

Upvotes: 1

Amr Lotfy
Amr Lotfy

Reputation: 2997

Source https://askubuntu.com/a/286651/113065

Edit -> preferences -> Behavior -> Executable Text Files = Run ...

It should run, but you can't see output.

Upvotes: 4

orvi
orvi

Reputation: 3340

Follow these steps:

  • Hit Alt+F2, type dconf-editor and hit ``Enter.

  • In dconfg-editor goto: 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!

Upvotes: 17

John Montgomery
John Montgomery

Reputation: 7096

You need to make it an executable file, either use chmod +x <filename> or go into the file properties and set it there.

Upvotes: 1

Related Questions