BASH correct usage of single/double quotes when writing variables to file (running Ubuntu 16.04.3)
I created small script with tasks I do every time I upgrade/change Ubuntu version. One of the tasks is to write script to file, this is actual written file:

#!/bin/bash

if [ "$1" == "eno1" ]; then
 case "$2" in
  up) nmcli radio wifi off
      notify-send "cable network detected, turning WIFI off" -u low -t 10;;
  down) nmcli radio wifi on
      notify-send "cable network unplugged, turning WIFI on" -u low -t 10;;
  esac
fi


And this is how I write it from another script:

MY_FILE="/etc/NetworkManager/dispatcher.d/99-wlan"
ni="$(nmcli dev status | grep ethernet | awk '{ print $1 }')"
echo -e '#!/bin/bash\n\nif [ "$1" == "'"$ni"'" ]; then\n case "$2" in\n'\
  ' up) nmcli radio wifi off\n      notify-send "cable network detected, turning WIFI off" --urgency critical --expire-time 6;;\n'\
  ' down) nmcli radio wifi on\n      notify-send "cable network unplugged, turning WIFI on" --urgency critical --expire-time 6;;\n'\
  ' esac\nfi' | sudo tee -a "$MY_FILE" &gt; /dev/null
sudo chmod +x "$MY_FILE"


My question is, if syntax I'm using is correct. When I check it with shellcheck utility, it complaints about first line:

echo -e '#!/bin/bash\n\nif [ "$1" == "'"$ni"'" ]; then\n case "$2" in\n'\
        ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.
                                            ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.


can I ignore this warnings or is there a better how to write it ? Keep in mind I'm using variable $ni when I'm writing 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).