Desktop shortcut to Bash script crashes and burns

The problem is the script relies on the TERM environmental variable being setup. The Ubuntu Unity Desktop does not have this initialized when scripts are called. If you open a terminal with Ctrl+Alt+T the variable is setup.

To test your system create a little script called test-term.sh and make it look like this:

#!/bin/bash

#See if $TERM has been set when called from Desktop shortcut

echo TERM environment variable: $TERM > ~/Downloads/test-term.txt
echo "Using env | grep TERM output below:" >> ~/Downloads/test-term.txt
env | grep TERM >> ~/Downloads/test-term.txt

exit 0

Create a link in Nautilus to test-term.sh and run the link. Then check the output file:

$ cat ~/Downloads/test-term.txt

TERM environment variable: dumb
Using env | grep TERM output below:
(... blank line appears here ...)

As you can see the environment variable TERM is blank when the command env | grep TERM is used. Also the variable $TERM is set to dumb which doesn't suit the color-based, mouse-supported command dialog very well.


Boilerplate solution

The short term solution was to include boilerplate code at the top of the two scripts in question:

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi