Setting global environment vars from Python or Bash
env | grep '^NOM'  would more exactly align with the sed command.

ensure you're exactly matching the variable name:

sudo sed -i "/^$VAR=/d" /etc/environment
# .................^


Use %-formatters for printf:

printf '%s="%s"\n" "$VAR" "$VAL"


In the function, use local to limit the scope of VAR and VAL



had another thought about that function. Change

sudo sed -i "/^$VAR/d" /etc/environment
printf "$VAR=$VAL\n" | sudo tee -a /etc/environment &gt; /dev/null


to

tmpfile=$(mktemp)
grep -v "^$VAR=" /etc/environment &gt; $tmpfile
echo "$VAR=$VAL" &gt;&gt; $tmpfile
sudo mv $tmpfile /etc/environment


That minimized any possibility that the /etc/environment file contains "invalid" data -- it overwrites the previous good file with the new good file all at once.