In macOS, environment variables are used to store system-wide or user-specific information that can be accessed by applications and scripts. Environment variables are widely used for configuring system settings, paths, and other preferences in shell sessions. macOS, being UNIX-based, allows you to set, view, and manage these environment variables using various methods.
1. Viewing Environment Variables
To view environment variables, you can use the printenv
or env
command in the terminal:
printenv
env
To view a specific environment variable, use echo
:
echo $VARIABLE_NAME
Example:
echo $PATH
2. Setting Environment Variables Temporarily
You can set an environment variable temporarily in the current terminal session:
export VARIABLE_NAME="value"
Example:
export MY_VAR="Hello World"
The variable will be available until you close the terminal session.
3. Setting Environment Variables Permanently
To make environment variables persistent across terminal sessions, you need to define them in one of the shell profile files. The specific file you use depends on the shell you are using.
For Bash Shell
If you are using Bash (default in macOS versions before Catalina):
~/.bash_profile
or~/.bashrc
: Add your environment variables here.
echo 'export MY_VAR="Hello World"' >> ~/.bash_profile
Then apply the changes:
source ~/.bash_profile
For Zsh Shell
If you are using Zsh (default in macOS versions Catalina and later):
~/.zshrc
: Add your environment variables here.
echo 'export MY_VAR="Hello World"' >> ~/.zshrc
Then apply the changes:
source ~/.zshrc
System-Wide Variables
For system-wide settings, you can add environment variables to /etc/profile
, /etc/bashrc
, or /etc/zshrc
. However, modifying these files affects all users, and administrative privileges are required.
4. Environment Variables for Applications
To set environment variables that are available for all applications (not just terminal shells), you can modify ~/.MacOSX/environment.plist
. However, this method is deprecated in newer macOS versions.
An alternative for setting environment variables for applications is to use launchctl
. Example:
launchctl setenv VARIABLE_NAME value
Note that this approach works at the session level for GUI applications.
5. Common Environment Variables
PATH
: Contains a list of directories where the system looks for executable files.HOME
: The path to the current user's home directory.SHELL
: The path of the default shell being used.USER
: The username of the current user.
6. Editing Files with Nano or Vim
You can edit your profile files using terminal text editors such as nano
or vim
.
For example, to edit ~/.zshrc
:
nano ~/.zshrc
Add your environment variable, save (CTRL + O), and exit (CTRL + X).
Example: Adding a Directory to PATH
Suppose you need to add /usr/local/myapp/bin
to your PATH
so that commands from that directory can be executed without specifying the full path:
- Open the relevant profile file (
~/.zshrc
or~/.bash_profile
). - Add the following line:
- Apply the changes:
export PATH=$PATH:/usr/local/myapp/bin
source ~/.zshrc
7. Shell Profiles Summary
- Bash Users (macOS Mojave and earlier): Modify
~/.bash_profile
or~/.bashrc
. - Zsh Users (macOS Catalina and later): Modify
~/.zshrc
. - System-Wide Configuration: Modify
/etc/profile
or/etc/zshrc
(requires sudo).
Conclusion
Managing environment variables on macOS is similar to other UNIX-based systems. You can define them temporarily using export
, or permanently by adding them to shell profile files such as ~/.zshrc
, ~/.bash_profile
, or /etc/profile
. Using these methods, you can easily configure your shell environment to suit your development or scripting needs.
Comments
Post a Comment