BASH correct usage of single/double quotes when writing variables to file
Instead of embedding sudo in the script, simply require that it be run as root, either by simply failing (using set -e), or with a friendly check such as

die() { echo "$@" &gt;&amp;2; exit 1; }

test $UID = 0 || die "$0: you need to be a superuser!"


That echo can be made much easier to read, and can avoid the non-portable -e option, by using a here-document:

cat &lt;&lt;END &gt;"$MY_FILE"
#!/bin/bash
if [ "\$1" == "$ni" ]; then
    case "\$2" in
        up)
            nmcli radio wifi off
            notify-send "cable network detected, turning WIFI off" --urgency critical --expire-time 6
            ;;
        down)
            nmcli radio wifi on
            notify-send "cable network unplugged, turning WIFI on" --urgency critical --expire-time 6
            ;;
    esac
fi
END


(I'm assuming you meant tee rather than tee -a in your code).