Linux GUI (GNOME) - run a shell script by clicking a desktop icon

By yannek, 22 December, 2024

Working with Linux GUI on my home laptop, I wanted to simplify the process of running some script. Basically, I wanted to run it by clicking a desktop icon. Surprisingly, I did not find it as straightforward as I expected. Step by step then. 

  Facts:

  • Rocky Linux release 8.10 (Green Obsidian)
  • GNOME 3.32.2

  Requirements:

  • Desktop Icons GNOME shell extension
  • Some tool to manage the GNOME extensions. I found GNOME Tweaks easy to accomplish that. 

Luckily, I had both packages already installed.

$ dnf list --installed 'GNOME*ICONS*' 'GNOME*TWEAKS*'
Installed Packages
gnome-shell-extension-desktop-icons.noarch        3.32.1-39.el8_10        @appstream
gnome-tweaks.noarch                               3.28.1-7.el8            @appstream

Steps:

  1. If you already have some icons on your desktop, you can skip this step. Otherwise, check if Desktop Icons GNOME extension is on.
Run GNOME Tweaks

 

Turn on Desktop Icons extension
  1. Let's say we have a classic Hello World ! script as follows

    $ cat ~/scripts/hello.sh 
    echo -e "\nHello World !\n"

    The way to run the script  via a desktop icon is based on Desktop Entry file that GNOME uses for defining menu item. You can find out more on those files here.
    For a test, create a desktop entry file in ~/Desktop directory. Its content is as follows

    [Desktop Entry]
    Version=1.0
    Name=Hello
    Comment=Hello World
    Exec=/bin/bash -c "~/scripts/hello.sh"
    Icon=call-start
    Terminal=true 
    Type=Application
    Categories=Education;

    The required filename extension is .desktop . The full path to the above desktop entry file is ~/Desktop/hello.desktop .

    Note the Exec key. Its value depends on your actual shell, and when a related binary is located. $SHELL environment variable holds that info for you.

    $ echo $SHELL
    /bin/bash
  2. Now, you should have a new icon on your desktop as follows

    new icon on a desktop

    At that moment, when I clicked the icon, the desktop entry file content was opened in the notepad app.

  3. Click the hello.desktop icon (right button) and select Allow Launching to make it executable.

    icon mark it launchable

    The icon should change to the one specified with the Icon key in the desktop entry file.

    Made executable

    Now, the script should be executed once you have double-clicked the icon. However, you may experience the script running almost unnoticeable. There may be just some log entries indicating that the gnome-terminal was run. At the moment when this article was being written, the following warnings appeared in the syslog (/var/log/messages).

    org.gnome.Shell.desktop[503767]: # Option “-x” is deprecated and might be removed in a later version of gnome-terminal.
    org.gnome.Shell.desktop[503767]: # Use “-- ” to terminate the options and put the command line to execute after it.

    You may want to see what was sent by the script to stdout, though. Here are some examples on how to achieve that by modifying the Exec key value in the desktop entry file.

    a) add sleep command to leave the terminal open for a specific amount of time (5s in the example below).

    Exec=/bin/bash -c "~/scripts/hello.sh; sleep 5"

    b) add an extra shell invocation. That will require you to close the terminal window manually though.

    Exec=/bin/bash -c "~/scripts/hello.sh; /bin/bash"

    c) add a request for pressing some key to close the terminal window.

    Exec=/bin/bash -c "~/scripts/hello.sh; key=''; echo \"Press 'x' to exit\"; while [ \"$key\" != 'x' ]; do read -s -n 1 key; done"

     

 Some extra hints at the end.

  1. You may want to assign some more relevant icon to the script. Search for available icons in /usr/share/icons/Adwaita
  2. If Allow Launching option is not available in the right-click menu for the desktop entry file, choose Properties, and then Permissions. It should be a tick box with Allow executing file as program text aside there.

Comments