Create a .desktop file that opens and execute a command in a terminal

I would like to know how to write the Exec command of a .desktop file to open a new terminal and execute a shell script in it. The shell script is working and accessible by all users. When launching the script from the terminal everything works, but it doesn't when trying to launch the script from a .desktop file.

Here are some combinations I have already tried:

Exec=gnome-terminal -x sh -c 'echo hello'
Exec=sh -c 'gnome-terminal echo hello'
Exec=sh -c 'echo hello'
Exec=echo hello

The .desktop terminal option is set to true.


Solution 1:

The content of your desktop file should look like (see how to create a .desktop file using a text editor):

[Desktop Entry]
Version=1.0
Name=Test        
Comment=Test the terminal running a command inside it
Exec=gnome-terminal -e "bash -c 'echo hello;$SHELL'"
Icon=utilities-terminal
Terminal=false
Type=Application
Categories=Application;

Or:

[Desktop Entry]
Version=1.0
Name=Test        
Comment=Test the terminal running a command inside it
Exec=bash -c 'echo hello;$SHELL'
Icon=utilities-terminal
Terminal=true 
Type=Application
Categories=Application;

In the first case, the Terminal field is set to false (perhaps contrary to your expectations) and in second case is set to true, but the result is in both cases the same.

Solution 2:

Simply add

;$SHELL 

at the end of your commands.

Like for me snapd isn't something using full bandwidth of system to refresh snaps anytime almost I work at night.

So this worked for me to create a .sh file linked to a .desktop file.
Contents for .sh file were

echo <your password> | sudo -S systemctl stop snapd.service
sudo systemctl disable snapd.service;$SHELL

-S in the first line of the .sh file is used to send STDINPUT to the sudo command meaning direct execute without entering password.

Contents for the .desktop file were:

[Desktop Entry]
Version=1.0
Name=Test
Comment=Test the terminal running a command inside it
Exec=gnome-terminal -e "/scripts/disable_snap.sh"
Icon=terminal
Terminal=true
Type=Application
Categories=Application;