Create a .desktop file that opens and execute a command in a terminal
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;
Solution 3
!#/bin/bash
gnome-terminal -e YOUR_COMMANDS
Make the above file. Don't forget to sudo chmod +x filename.sh
Solution 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'
-
sh -c 'COMMAND'
run the "sh" binary found in $PATH which executes COMMAND (on many systems,sh
is the symbolic link tobash
, but for portability "sh" is prefered) -
echo hello; $SHELL
does two things. First, it runsecho 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.
Related videos on Youtube
Paradiesstaub
Updated on September 18, 2022Comments
-
Paradiesstaub over 1 year
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.
-
Paradiesstaub about 10 yearsThe problem with this solution is, that the terminal disappears immediately and a user is not capable to enter something.
-
Mr.Lee about 10 yearsi forgot what it is called but I i think there is a option to keep the terminal up. It might be
hold
-
hatterman about 10 yearsIt 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 about 10 yearsAre 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 ?
-
Paradiesstaub about 10 yearsThe first solution works for me, so I stick with it. Do you know why there has to be $SHELL?
-
E_Angel over 7 yearsIt 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 ?
-
code_dredd about 6 years@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.)
-
RockScience about 6 yearsI 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
-
Adrian Keister almost 4 yearsI 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.
-
Julian Coffee almost 4 years@AdrianKeister, it is opened, but in another terminal? And what launcher (or DE) are you using?
-
Michael over 3 years
Terminal=true
doesn't open the terminal for me in gnome, althoughgnome-terminal
does work in a terminal to open another terminal.