63

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.

Rinzwind
  • 309,379

5 Answers5

76

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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
7

TL;DR

[Desktop Entry]
Version=1.0
Name=Hello
Exec=sh -c 'echo hello; $SHELL'
Icon=utilities-terminal
Terminal=true
Type=Application

As stated in desktop entry specification, Terminal=true tells the launcher to launch your script in a terminal window. The chosen Terminal Emulator depends on your default applications settings and Desktop Environment. In GNOME, it is gnome-terminal, in KDE, it is Konsole. (Without DE, in plain WM there is a bug in xdg-open, and Terminal=true just ignored, see issue)

You need this line to run your script and launch a shell after it.

Exec=sh -c 'echo hello; $SHELL'
  1. sh -c 'COMMAND' run the "sh" binary found in $PATH which executes COMMAND (on many systems, sh is the symbolic link to bash, but for portability "sh" is prefered)
  2. echo hello; $SHELL does two things. First, it runs echo hello and then, after the execution of this command, launches $SHELL.

If you will not add some command that waits for you to exit, a terminal emulator will just run your program and exit.


P.S. If you just want not to close your terminal, you can do

Exec=sh -c 'echo hello; read'

Or

Exec=sh -c 'echo hello; sleep 5'

(read will wait for you to press Enter, sleep will just wait 5 seconds)

P.P.S $SHELL probably will be the same as last section in /etc/passwd in line with your user.

$ man login

The value for $HOME, $USER, $SHELL, $PATH, $LOGNAME, and $MAIL are set ac‐ cording to the appropriate fields in the password entry.

3

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;
zx485
  • 2,865
user225
  • 41
  • 1
3
!#/bin/bash

gnome-terminal -e YOUR_COMMANDS

Make the above file. Don't forget to sudo chmod +x filename.sh

Mr.Lee
  • 891
0

I do this. First line of the text file is !#/bin/bash

Subsequent lines of the text file are the commands (the shell script).

Save the file as something.sh

Open the properties of the file and enable run file as a program.

Now, when the file is double clicked, I get the option to run it.

hatterman
  • 2,330