[This post has been edited from the original text to add the results of further investigation and get to the point more quickly -- Thanks @ctrl-alt-delor]
Why does this script:
#!/bin/bash read -p "Press any key to continue: " key nohup java -jar /path/to/compiled.jar &
not successfully start/display the specified java GUI app when it is invoked by a Terminal window opened as a result of double-clicking the following desktop shortcut:
[Desktop Entry] Icon=bash Exec=bash /path/to/the/above/script.sh Type=Application Terminal=true
when it works as expected when manually invoked from a manually-opened Terminal window?
Surprisingly, if the script is modified by merely adding a
sleep 0 command, to become:
#!/bin/bash read -p "Press any key to continue: " key nohup java -jar /path/to/compiled.jar & sleep 0
then it works as expected regardless of how it is invoked.
My question for this post is: Why is the sleep command required? Can anyone explain this behavior?
I believe this question is subtly different from StackExchange question 3886 and also different from all of the dozen or so others that appear to be similar because none of them refer to the case of script invocation from a desktop shortcut/launcher.
Do not be concerned with the apparent absurdity of the script. I have intentionally simplified it for the purposes of this post to provide the simplest example that will reproduce the issue without getting distracted by other issues.
Subsequent testing has shown that the behavior is the same regardless of whether or not the command to invoke java redirects either or both stdout and sdterr: the java app always displays when the script is manually invoked from a manually-opened Terminal window, but when invoked from a desktop shortcut, it works only if a sleep command is included when the script is launched from a desktop shortcut.
Examination of the Terminal environment (using
printenv as suggested by @Michael Prokopek) reveals no differences except for the GNOME_TERMINAL_SERVICE and GNOME_TERMINAL_SCREEN variables which appear to be unique for each Terminal window.
BTW: Works like a charm with no drama at all using a DOS script on Windows.