Practical Env Vars Usage
Setting Environment Variables
Temporary Variable Setting
## Set variable for current session
$ MYVAR="Hello LabEx"
## Use the variable
$ echo $MYVAR
Hello LabEx
Persistent Variable Setting
## Modify .bashrc for permanent setting
$ echo 'export MYVAR="Hello LabEx"' >> ~/.bashrc
## Reload configuration
$ source ~/.bashrc
Common Use Cases
1. Path Configuration
## Add custom directory to PATH
$ export PATH=$PATH:/home/user/custom/bin
2. Application Configuration
## Set default text editor
$ export EDITOR=vim
## Configure language settings
$ export LANG=en_US.UTF-8
Environment Variable Scopes
graph TD
A[Environment Variable Scopes] --> B[Shell Session]
A --> C[User Profile]
A --> D[System-wide]
B --> E[Temporary]
C --> F[Persistent for User]
D --> G[Affects All Users]
Variable Management Techniques
Operation |
Command |
Description |
Set Variable |
export VAR=value |
Create/modify variable |
Unset Variable |
unset VAR |
Remove variable |
View Variable |
echo $VAR |
Display variable value |
Security and Best Practices
## Avoid storing sensitive data in plain text
$ export API_KEY="$(cat /secure/location/key)"
Variable Naming Conventions
- Use UPPERCASE for environment variables
- Use descriptive and meaningful names
- Avoid special characters
LabEx Pro Tip
In LabEx Linux environments, understanding environment variable management is crucial for system configuration and application development.
Advanced Scripting Example
#!/bin/bash
## Dynamic environment configuration script
## Check if variable exists
if [ -z "$CUSTOM_PATH" ]; then
export CUSTOM_PATH="/default/path"
fi
## Conditional configuration
if [ "$ENV" = "production" ]; then
export DEBUG=0
else
export DEBUG=1
fi
Debugging Environment Variables
## Troubleshoot variable issues
$ env | grep PROBLEMATIC_VAR
$ echo $PROBLEMATIC_VAR