12

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.

4
  • 2
    What window manager are you using (e.g. gnome, kde, etc..)? Are you clicking on the name in a file manager window? I assume you're double-clicking an icon representing the file on the desktop or in a file browser, and not just clicking the filename in a terminal window (particularly because you indicate something actually happens).
    – clearlight
    Commented Feb 4, 2017 at 23:06
  • Assuming you're using GTK, check this out askubuntu.com/a/664272
    – clearlight
    Commented Feb 4, 2017 at 23:11
  • I'm voting to close this question as off-topic because it is about Linux so should be asked on unix.stackexchange.com
    – Rob
    Commented Feb 15, 2017 at 18:17
  • @rob Yes, you can close this question here. I would copy it to unix.stackexchange.com
    – pnkjmndhl
    Commented Feb 15, 2017 at 18:21

5 Answers 5

17

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!

6
  • Entering: dconf-editor and hitting enter doesn't do anything. I am using Linux Ubuntu16.04
    – pnkjmndhl
    Commented Feb 6, 2017 at 18:54
  • check this link then askubuntu.com/questions/605965/…
    – orvi
    Commented Feb 6, 2017 at 19:31
  • Can you explain it here. I cant understand whats there. I just want it to be an executable script that runs just by double clicking it.
    – pnkjmndhl
    Commented Feb 15, 2017 at 18:11
  • 3
    install dconf-editor by doing this command sudo apt-get install dconf-tools
    – orvi
    Commented Feb 15, 2017 at 18:23
  • Nice answer +1, but I found that when it uses "launch" it runs in the background so you can't see what is going on. I wanted it to run in a terminal - so if you select "ask" then it asks you to run in a terminal or just open it - selecting run in terminal does as it says which I found useful : ) Commented Aug 20, 2019 at 18:11
4

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

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

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

2
  • is there a way to configure ubuntu to show the output?
    – bjubes
    Commented Feb 24, 2022 at 21:22
  • @bjubes drag the file into a terminal window and press enter
    – majjam
    Commented Oct 26, 2022 at 14:08
3

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
3
  • This is an interesting answer. I guess it is worth mentioning that a more straightforward version of this is to create one script file, and one .desktop file that references the script in its Exec field, and for me personally, that would be the best answer on this page. Commented Jan 19, 2023 at 4:01
  • That would involve two files, while the question effectively ask for one single file. The solution I proposed can be set up by a developer and it will work for any user out of the box, without any permission/setting change needed.
    – fortea
    Commented Feb 3, 2023 at 13:57
  • I don't think it's fair to penalize this technique for using "two files". A creator, using this technique, only writes one file. The distribution of this technique only sends one file. The end-user of this technique only interacts with one file. They are all the same file! The fact a temporary gets created during the use seems like a very innocuous implementation detail, for the large majority of people interested in this question, even, potentially, the OP. Commented Feb 7, 2023 at 17:33
1

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

2
  • It is executable (I have already done that). But when I try to open it, it opens in editing mode.
    – pnkjmndhl
    Commented Feb 4, 2017 at 19:48
  • I'm using Ubuntu 17.10 and these solutions do not work anymore. As they say, it will only open in the selected application and there is no option to change the application to execute or terminal. Does anyone have a new solution? Thanks.
    – suze1992
    Commented Jul 20, 2018 at 2:20
1
  1. Open nautilus
  2. Behavior tab
  3. Executable Text files -> Ask each time
  4. Profit

Source. Tested on Ubuntu 20.04.

1
  • Yes, you are right, just Ububtu can not auto run via Terminal and I don't know way to do this. In option Open With not show terminal to select, too
    – tquang
    Commented Nov 3, 2021 at 7:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.