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

command linedesktopscripts

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.

Best Answer

  • 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.