Chapter 9: Environment Variables and Configuration Management¶
Learning Objectives¶
By the end of this chapter, you will be able to:
- Understand the difference between user and system environment variables on macOS.
- Configure and manage
.bash_profileand.bashrc, including macOS-specific considerations. - Use Launch Agents and Daemons to manage environment variables and scripts.
Introduction: Why Environment Configuration Matters¶
Environment variables influence how Bash scripts run and interact with macOS. They define system-wide paths, user preferences, and credentials. Misconfigurations can lead to unexpected behavior, security issues, or broken automation. This chapter shows you how to manage these settings correctly.
9.1 User vs System Environment¶
- User Environment Variables: Defined in your user’s shell config files like
.bash_profileor.bashrc. They apply only when you run a shell as your user. - System Environment Variables: Defined in system files like
/etc/profileor/etc/paths. They affect all users and sometimes system daemons.
Check your current environment:
Or print a single variable:
Modifying System Variables Safely¶
- On macOS, modify
/etc/pathsto add system-wide directories. - For sensitive scripts, prefer user-level variables unless you need system scope.
9.2 .bash_profile, .bashrc, and macOS Quirks¶
Traditionally:
.bash_profileruns for login shells..bashrcruns for interactive non-login shells.
macOS Terminal launches login shells by default, so your .bash_profile is often the right place to export variables.
Example .bash_profile:
To ensure .bashrc runs, source it in .bash_profile:
Tip: Always test changes with a new Terminal window.
9.3 Managing Config with Launch Agents and Daemons¶
macOS uses launchd to manage user and system processes. Sometimes you need to ensure environment variables are available to GUI apps or background tasks.
- Launch Agents run as a user.
- Launch Daemons run as root.
Example: Create a user Launch Agent to set a variable and run a script.
- Create a
plistfile in~/Library/LaunchAgents/:
<?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>com.example.setenv</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>export MY_VAR=hello && echo $MY_VAR > /tmp/my_var.txt</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
- Load the agent:
- Verify the variable was set:
Tip: For system-wide daemons, use /Library/LaunchDaemons/ and ensure permissions are correct.
Chapter 9 Exercise¶
Manage your environment:
- Add a custom
PATHor variable to your.bash_profile. - Test with
envandecho. - Create a Launch Agent that runs a small script and sets a variable.
- Inspect the logs with
log showorConsole.
Example shell snippet:
macOS Scripting Tips¶
- Always back up
.bash_profileand.bashrcbefore changes. - Use descriptive variable names.
- Test with a new shell session to confirm behavior.
- Use
launchctl listto verify active agents and daemons.