Linux Environment Variables
🔍 What Are Environment Variables?
Environment variables are key-value pairs that provide information about the system environment to processes. They are inherited by child processes and can influence the execution of commands and applications
Common environment variables include:
PATH
:Specifies directories to search for executable filesHOME
:Indicates the current user’s home directoryUSER
:Stores the name of the current userSHELL
:Defines the path to the current user’s shellLANG
:Sets the system’s locale and language settings
🧰 Managing Environment Variables
1. Listing Environment Variables
To display all environment variable:
printenv
``
Or, to display a specific variabl:
```bash
echo $VARIABLE_NAME
For example, to check the PATH
variable:
echo $PATH
2. Setting Environment Variables Temporarily
To set an environment variable for the current session:
export VARIABLE_NAME="value"
For example:
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk"
This variable will be available until the session end.
3. Setting Environment Variables Permanently
To make an environment variable persistent across sessions, add it to your shell’s initialization file:
For Bash:
echo 'export VARIABLE_NAME="value"' >> ~/.bashrc
source ~/.bashrc
For Zsh:
echo 'export VARIABLE_NAME="value"' >> ~/.zshrc
source ~/.zshrc
For system-wide variables, add them to /etc/environment
or create a script in /etc/profile.d/
:
echo 'export VARIABLE_NAME="value"' | sudo tee /etc/profile.d/custom_env.sh
sudo chmod +x /etc/profile.d/custom_env.sh
🧠 Best Practices
**Use uppercase names*: By convention, environment variable names are uppercase to distinguish them from shell variables.
**Be cautious with
PATH
*: When modifyingPATH
, append directories rather than replacing its value to avoid overwriting existing paths.**Secure sensitive data*: Avoid storing sensitive information like passwords directly in environment variables. Use secure methods for handling such data.
**Document changes*: Keep track of modifications to environment variables, especially system-wide changes, to maintain system integrity.
🛠 Troubleshooting
Variable not found: Ensure the variable is defined in the correct initialization file and that the file is sourced.
Changes not reflected: After modifying initialization files, run
source ~/.bashrc
(or the appropriate file) to apply chances.Conflicting variables: Check for conflicting definitions in multiple files and resolve them to prevent unexpected behavior.