Setting a variable without logging out
A modern GNOME session runs under systemd; GUI applications inherit that environment:
systemctl --user set-environment MY_VAR=value
dbus-update-activation-environment --systemd MY_VAR=value
The first sets it for systemd user services and later shell-launched applications; the second makes D-Bus-activated apps see it, how most GNOME apps start.
echo $MY_VAR prints nothing: variables are copied into a process at start, and your shell predates the change. Shell and systemd user session do not propagate:
systemctl --user show-environment | grep MY_VAR
eval "$(systemctl --user show-environment | grep MY_VAR)"
The first confirms the setting, the second imports it into the current shell.
Persistent, in a drop-in read at session start:
echo 'MY_VAR=value' >> ~/.config/environment.d/myenv.conf
systemctl --user import-environment MY_VAR
dbus-update-activation-environment --systemd MY_VAR
Where not to put it: ~/.profile and ~/.bashrc need a new login, /etc/environment a logout or reboot, and /etc/profile reaches shells only, never the GNOME session.
Running something at login
Autostart, for a GUI program or one needing the session environment — DISPLAY, DBUS_SESSION_BUS_ADDRESS:
mkdir -p ~/.config/autostart
[Desktop Entry]
Type=Application
Exec=/path/to/your/script.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=My Login Script
Comment=Run my script at GNOME login
chmod +x /path/to/your/script.sh
A systemd user service, for background, restartable work — more reliable than autostart for anything long-lived:
[Unit]
Description=Run my script at GNOME login
After=graphical-session.target
[Service]
ExecStart=/path/to/your/script.sh
Type=simple
[Install]
WantedBy=default.target
systemctl --user daemon-reload
systemctl --user enable --now myscript.service
A shell RC file, for a text login before GNOME: ~/.bash_profile or ~/.bash_login. Anything needing the GNOME session will not work.
Anything requiring sudo will not run at login without passwordless sudo — rewrite it as a system service.