Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
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:
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.
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:
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.
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:
The window ends up containing all the information:
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.
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:
For instance, we can get the below window after clicking twice on the test.sh script:
This way, we execute the script by double-clicking and confirming the action with another single click.
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.