Skip to content

Instantly share code, notes, and snippets.

@MrPandir
Last active February 16, 2026 17:03
Show Gist options
  • Select an option

  • Save MrPandir/d591a6f372fc4cbd835fd214825a3070 to your computer and use it in GitHub Desktop.

Select an option

Save MrPandir/d591a6f372fc4cbd835fd214825a3070 to your computer and use it in GitHub Desktop.
Setting environment variables globally on MacOS at login.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>my.startup</string>
<key>Program</key>
<string>/Users/user/.config/login_setenv.sh</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
#!/bin/sh
launchctl setenv XDG_CONFIG_HOME $HOME/.config
launchctl setenv XDG_DATA_HOME $HOME/.local/share
launchctl setenv XDG_STATE_HOME $HOME/.local/state
launchctl setenv XDG_CACHE_HOME $HOME/.cache
launchctl setenv XDG_RUNTIME_DIR $TMPDIR
launchctl setenv ZDOTDIR $HOME/.config/zsh
launchctl setenv TERMINFO $HOME/.local/share/terminfo
launchctl setenv TERMINFO_DIRS $HOME/.local/share/terminfo:/usr/share/terminfo
launchctl setenv WAKATIME_HOME $HOME/.local/share/wakatime
@MrPandir
Copy link
Author

Guide

  1. Place environment.plist into the directory ~/Library/LaunchAgents
  2. Place login_setenv.sh wherever you like.
  3. Make the file executable: chmod +x login_setenv.sh
  4. In the environment.plist file, replace /Users/user/.config/login_setenv.sh with the actual path to your login_setenv.sh file.
  5. The script will be executed automatically on next login.

Useful commands

Note

501 is your current user ID (UID). You can get it with the command: id -u

  • launchctl bootout gui/501/my.startup
    → Unloads / stops the service (removes it from running state)

  • launchctl bootstrap gui/501 ~/Library/LaunchAgents/environment.plist
    → Registers / loads the plist into launchd (makes the service known to the system)

  • launchctl kickstart gui/501/my.startup
    → Immediately starts the service (even if it has no KeepAlive / RunAtLoad)

  • launchctl print gui/501/my.startup
    → Shows detailed status and properties of this particular service

  • launchctl print gui/501
    → Shows all services and their state for current user domain

  • launchctl list | grep -i my.startup
    → Shows whether the service is currently loaded and its PID (if running)

  • launchctl getenv XDG_CONFIG_HOME
    → Shows the value of environment variable from launchd's perspective

  • printenv | grep -i '^XDG_'
    → Shows all XDG_* variables visible in the current shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment