58

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.

0

5 Answers 5

69

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.

4
  • 1
    The first solution works for me, so I stick with it. Do you know why there has to be $SHELL? Mar 24, 2014 at 13:28
  • It worked for me , however i want to launch a command with sudo , then when use launcher created it opens terminal asking-me for password . How do i launch a command and prevent this behavior ?
    – E_Angel
    Dec 13, 2016 at 13:04
  • 1
    @EliasAlves A bit late, but for anyone else reading this: you cannot expect to run commands that require root privileges without being required to authenticate yourself. (Unless you explicitly configure your system to do that, which is generally a bad idea.)
    – code_dredd
    Mar 29, 2018 at 20:56
  • 2
    I think the question is: how to make sure the terminal appears so that the terminal doesn't disappear so that we can enter the credentials Apr 8, 2018 at 8:21
4

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
  • 2
    I do not think your statement, "The chosen Terminal Emulator depends on your default applications settings and Desktop Environment." is true. I have Xterm as my default terminal, but using 'Terminal=true' doesn't open in Xterm. How would this be enforced, here? It seems like this should be expected behavior. Jun 4, 2020 at 16:32
  • @AdrianKeister, it is opened, but in another terminal? And what launcher (or DE) are you using? Jun 4, 2020 at 17:42
  • Terminal=true doesn't open the terminal for me in gnome, although gnome-terminal does work in a terminal to open another terminal.
    – Michael
    Aug 23, 2020 at 19:58
3
!#/bin/bash

gnome-terminal -e YOUR_COMMANDS

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

2
  • 1
    The problem with this solution is, that the terminal disappears immediately and a user is not capable to enter something. Mar 20, 2014 at 15:47
  • i forgot what it is called but I i think there is a option to keep the terminal up. It might be hold
    – Mr.Lee
    Mar 20, 2014 at 18:37
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;
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.

2
  • It works for me no problems. Create the shell script, add the line !#/bin/bash as the first line. Allow the file to be run as an executable. Double click and select run in terminal. It just works.
    – hatterman
    Mar 21, 2014 at 21:21
  • 1
    Are you sure the terminal isn't just closing because the shell script has finished ? At the beginning of your script put sleep 30, does the terminal stay open for 30 seconds ?
    – hatterman
    Mar 23, 2014 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .