1. Overview

Double-clicking is a common and straightforward way to open applications and scripts. Since scripts for cleanup, log analysis, backup and restore, and other tasks are common in everyday use, using a double click to run them can be a convenience to many users.

Additionally, double-clicking often avoids the command line, which makes working with scripts easier for users who are new to the terminal. This approach is also especially advantageous when sharing scripts or making shortcuts for frequently used tasks. Thus, it can improve the overall user experience.

In this tutorial, we’ll see how to launch a shell script by double-clicking on Ubuntu 22.04.

2. Preparing a Sample Script

Let’s create a simple script, test.sh, to demonstrate the methods for launching a script by double-clicking. Here, we see the contents of the script via the cat command:

$ cat > test.sh
#!/bin/bash
echo "Hello, world!"
read -p "Press Enter to continue..."

Let’s summarize the above script:

  • #!/bin/bash: shebang that tells the system to interpret the file with the Bash shell
  • echo: prints the text to the standard output
  • read -p: outputs the given string as a prompt prior to taking input from the user

Notably, we’ve put the script in our home directory. However, there are some best practices for the script location. Regardless, we need to note the path of our script. This will be used later.

Next, we need to make the script executable:

$ chmod +x test.sh

This ensures we’ve got the necessary permissions to execute the script.

3. Using Desktop Entry Files

Desktop entry files or .desktop files provide an efficient way to launch scripts by double-clicking. So, let’s create a desktop shortcut (or .desktop file) for our shell script.

First, we create a file test.desktop in the Desktop directory:

[Desktop Entry]
Name=Test
Comment= Script File Demo
Exec=/home/user/test.sh
Icon=/usr/share/test.png
Terminal=true
Type=Application

Moving on, we need to assign launching permission to this file. For this, we right-click the .desktop file and select the option Allow Launching:

assigning launch permission to .desktop file

Critically, different Linux desktop environments, such as GNOME, KDE, XFCE, and others may place the option to allow launching in different menus or system settings.

Finally, we can double-click on the resulting test.desktop Desktop file to launch our script.

4. Using make-launcher Snap Utility

We can also use the make-launcher snap package to launch a shell script by double-clicking. make-launcher* is a snap package that allows us to create launchers for applications on the *Desktop.

Let’s install make-launcher using the snap install command:

$ sudo snap install make-launcher

Once installed, we can start make-launcher from the application menu or by using the make-launcher command in the terminal. On the main window of the application, we fill in the required fields:

  • Program Name: Test
  • Command or path to file: /home/user/test.sh
  • Terminal app(true/false): true
  • Icon (path):

The window ends up containing all the information:

Make-Launcher Main Window

When we press Return or click on OK, the program creates a launcher file on the Desktop called Test.desktop. Again, we need to give this file permission to be run. To do this, we right-click the .desktop file and choose Allow Launching or follow the steps to do so in our current desktop environment.

As a result, our script can start with double-clicking. Similarly, we can also create launchers for other applications.

5. Shell Script Execution Prompt

In many Linux desktop environments, a dialog or prompt could show up when we double-click a shell script file. This prompt asks us about the action we want to perform:

  • run the script in the terminal
  • display the script (usually in the default text editor)
  • cancel our initial action
  • run the script directly

For instance, we can get the below window after clicking twice on the test.sh script:

Script Execution Control Dialog

This way, we execute the script by double-clicking and confirming the action with another single click.

6. Conclusion

In this article, we saw how to launch a shell script by double-clicking.

First, we learned how to use .desktop files manually. Then, we used a GUI application make-launcher to generate them. Overall, both ways used the same approach, i.e., creating .desktop files. However, the latter one is usually easier.

Finally, we discussed the basic prompt that many desktop environments show when double-clicking a script file.