Environment Variables and PATH
in Linux
Environment variables are dynamic values used by the shell and programs to configure behavior. They are stored as key-value pairs and affect how processes run.
Common Environment Variables
Variable | Description |
---|---|
PATH | Directories to search for executable commands |
HOME | Path to the current user’s home directory |
USER | Username of the current user |
SHELL | Default shell used by the user |
PWD | Present working directory |
EDITOR | Default text editor |
LANG | System language/locale settings |
Viewing Environment Variables
printenv # List all environment variables
echo $HOME # Show value of a specific variable
env # Another way to list current environment
Setting Environment Variables
Temporary (for current session only):
VARIABLE_NAME=value
Example:
MY_VAR=hello
echo $MY_VAR # Output: hello
Exporting (to make it available to subprocesses):
export VARIABLE_NAME=value
Example:
export MY_NAME="Alice"
Making Variables Permanent
To persist variables across sessions, define them in:
~/.bashrc
or~/.bash_profile
(for Bash)~/.zshrc
(for Zsh)
Example:
export MY_NAME="Alice"
Then reload:
source ~/.bashrc
The PATH
Variable
What is PATH
?
PATH
is an environment variable that tells the shell where to look for executable programs.
echo $PATH
Typical output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Adding a Directory to PATH Temporarily:
export PATH=$PATH:/your/custom/path
Example:
export PATH=$PATH:/opt/myapp/bin
Making PATH Change Permanent:
Add to your shell config file:
echo 'export PATH=$PATH:/opt/myapp/bin' >> ~/.bashrc
source ~/.bashrc
Best Practices
- Use
export
for variables needed by subprocesses. - Avoid overwriting
PATH
entirely; always append or prepend. - Use lowercase for custom temporary variables to avoid conflicts (e.g.,
my_var
).
Last updated on