π Environment Variables β Core Customization Technique in Bash
π― What You Will Learn
- Understand what environment variables are and how they function in Bash.
- Learn how to create, view, and persist environment variables.
- Discover the difference between shell variables and environment variables.
- Identify where these variables are stored (user-level and system-wide) β shown in tree view.
- Apply environment variables to WordPress VPS automation, such as defining global paths, credentials, and system behavior.
- Follow best practices for security, organization, and troubleshooting.
1. What Are Environment Variablesβ
An environment variable is a named value stored in memory that defines how processes and applications behave in your Linux environment.
They provide context to the shell, system services, and programs.
For example, $PATH, $HOME, and $USER are all environment variables.
Example:
echo $USER
Output:
root
These variables influence command behavior without editing scripts directly.β
2. Why Environment Variables Matterβ
| Reason | Description |
| Configuration | Set default behavior for all commands (e.g., editor, language). |
| Portability | Make scripts work across multiple systems without hardcoding paths. |
| Security | Store tokens or credentials safely (avoiding direct exposure in scripts). |
| Automation | Let scripts or WP-CLI commands access predefined paths or settings. |
| Performance | Avoid repeated setup by loading environment variables automatically. |
Example:
export EDITOR=nano
export PATH=$PATH:/usr/local/bin
Now every program using $EDITOR will open with nano, and your shell can find executables in /usr/local/bin.β
3. Where Environment Variables Are Storedβ
Environment variables can be temporary (in-memory only) or persistent (saved in configuration files).
| Scope | File | Applies To | Loaded When |
| Temporary (session only) | Defined directly in terminal | Current session | Disappears after logout |
| User-specific (persistent) | ~/.bashrc, ~/.bash_profile, or ~/.profile | One user | At login or shell start |
| System-wide (persistent) | /etc/environment or /etc/profile.d/*.sh | All users | During login |
4. File Locations (Tree View)β
User-level Configuration
/home/
βββ donnyariw/
βββ .bashrc
βββ .bash_profile
βββ .bash_aliases
βββ .bash_functions
βββ .profile
System-wide Configuration
/etc/
βββ environment
βββ profile
βββ bash.bashrc
βββ profile.d/
βββ lang.sh
βββ wp-tools.sh
βββ custom-vars.sh
β Recommendation:
- Use
~/.bashrcfor personal and safe variable definitions. - Use
/etc/environmentor/etc/profile.d/for shared VPS-wide configuration.
5. Viewing Environment Variablesβ
a. List all environment variablesβ
printenv
or:
env
b. Show a specific variableβ
echo $PATH
Output example:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/wp-tools
c. Show all shell and environment variablesβ
set
(includes both user variables and environment variables)β
6. Creating Environment Variablesβ
a. Temporary Variable (current session only)β
MYNAME="Donny"
echo $MYNAME
Output:
Donny
Lost when you close the terminal.β
b. Exporting a Variableβ
To make a variable available to child processes:
export MYNAME="Donny"
You can check:
printenv MYNAME
Output:
Donny
c. Persistent Variable (user-level)β
Add to your ~/.bashrc or ~/.bash_profile:
export EDITOR=nano
export HISTSIZE=10000
export PATH=$PATH:/root/wp-tools
export WP_ENV="production"
Reload your shell:
source ~/.bashrc
d. Persistent Variable (system-wide)β
Edit the global environment file:
sudo nano /etc/environment
Add:
EDITOR=nano
LANG=en_US.UTF-8
WP_ENV=production
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/wp-tools"
or create a dedicated script:
sudo nano /etc/profile.d/custom-vars.sh
Content:
export WP_ENV="production"
export WP_CLI_CACHE_DIR="/root/.wp-cli/cache"
Make executable:
sudo chmod +x /etc/profile.d/custom-vars.sh
7. Common Default Environment Variablesβ
| Variable | Purpose | Example Value |
$USER | Current logged-in username | root |
$HOME | Home directory | /root |
$SHELL | Default shell path | /bin/bash |
$PATH | Search directories for executables | /usr/local/bin:/usr/bin:/bin |
$PWD | Current directory | /root |
$LANG | Language/locale setting | en_US.UTF-8 |
$EDITOR | Default text editor | nano |
$HISTSIZE | Command history size | 10000 |
8. WordPress VPS Use Casesβ
| Variable | Example | Purpose |
WP_ENV | export WP_ENV=production | Distinguishes between staging/production environments. |
WP_CLI_CACHE_DIR | /root/.wp-cli/cache | Sets WP-CLIβs global cache location. |
DB_BACKUP_DIR | /root/wpbackup | Centralizes backup path for automation scripts. |
EDITOR | nano | Defines default CLI editor for WP or config editing. |
PATH | PATH=$PATH:/root/wp-tools | Adds custom WP tools directory to command search path. |
Example:β
export DB_BACKUP_DIR="/root/wpbackup"
wp db export $DB_BACKUP_DIR/db-$(date +%F).sql --allow-root
This method allows your scripts to remain portable and dynamic.β
9. Managing and Testingβ
a. Verify variable:β
echo $WP_ENV
b. Check if a variable exists:β
printenv | grep WP_ENV
c. Remove a variable:β
unset WP_ENV
d. Make available to subshells:β
export WP_ENV
10. Best Practicesβ
- Never store passwords or sensitive tokens in plain-text files like
.bashrc. - Use
/etc/environmentonly for simple key=value pairs (no commands). - Group related variables together logically.
- Always export variables that scripts or child processes will need.
- For WP automation, prefer descriptive naming (
WP_ENV,WP_BACKUP_PATH). - Test persistence after reboot or login/logout.
- Document variable purpose in comments.
11. Troubleshootingβ
| Problem | Cause | Solution |
| Variable not available in new session | File not sourced | Run source ~/.bashrc |
| PATH not applied system-wide | Forgot to export or reboot | Add to /etc/profile.d/ and reload |
| Cron job not reading variable | Cron runs with limited environment | Define variables directly inside cron or script |
| Variables overwritten | Conflicts between files | Check precedence order (system files load first) |
| Export error | Incorrect syntax | Use export VAR=value (no spaces around =) |
12. Quick Labβ
Objective: Define and verify environment variables for WordPress automation.
- Edit
.bashrc:
nano ~/.bashrc
- Add:
export DB_BACKUP_DIR="/root/wpbackup"
export WP_ENV="production"
export PATH=$PATH:/root/wp-tools
- Apply changes:
source ~/.bashrc
- Verify:
echo $WP_ENV
echo $DB_BACKUP_DIR
echo $PATH
- Test with WP-CLI:
wp db export $DB_BACKUP_DIR/db-$(date +%F).sql --allow-root
β WordPress database backup runs using your custom environment variable path.β
13. Glossaryβ
| Term | Definition |
| Environment Variable | A dynamic value that configures system or program behavior. |
| Shell Variable | A variable local to the current shell (not exported). |
| Export | Command to make a variable visible to child processes. |
| Persistent Variable | A variable saved to configuration files that reload automatically. |
| Subshell | A new shell instance that inherits exported variables. |
14. Mini-Quizβ
- Whatβs the difference between
set,env, andprintenv? - How do you make a variable available system-wide?
- What file is recommended for defining per-user environment variables?
- How do you make a variable available to child processes?
- Why shouldnβt you store passwords in
.bashrc?
15. Summaryβ
- Environment variables are key-value pairs controlling the systemβs runtime behavior.
- They exist in layers β temporary, user-level, and system-wide.
- User-specific variables live in
~/.bashrc,.bash_profile, or.profile. - Global variables go in
/etc/environmentor/etc/profile.d/*.sh. - Ideal for WordPress VPS automation, such as defining backup directories, cache paths, and global tool access.
- Always export variables when they must be accessible to scripts or subshells, and use secure methods for sensitive data.
Would you like me to continue next with βPrompt Customization (PS1 Variable)β, including file locations, hierarchy, and WordPress/VPS-focused prompt examples (production vs staging color cues)?