Skip to main content

πŸ“˜ Environment Variables β€” Core Customization Technique in Bash


🎯 What You Will Learn


  1. Understand what environment variables are and how they function in Bash.
  2. Learn how to create, view, and persist environment variables.
  3. Discover the difference between shell variables and environment variables.
  4. Identify where these variables are stored (user-level and system-wide) β€” shown in tree view.
  5. Apply environment variables to WordPress VPS automation, such as defining global paths, credentials, and system behavior.
  6. 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​

ReasonDescription
ConfigurationSet default behavior for all commands (e.g., editor, language).
PortabilityMake scripts work across multiple systems without hardcoding paths.
SecurityStore tokens or credentials safely (avoiding direct exposure in scripts).
AutomationLet scripts or WP-CLI commands access predefined paths or settings.
PerformanceAvoid 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).

ScopeFileApplies ToLoaded When
Temporary (session only)Defined directly in terminalCurrent sessionDisappears after logout
User-specific (persistent)~/.bashrc, ~/.bash_profile, or ~/.profileOne userAt login or shell start
System-wide (persistent)/etc/environment or /etc/profile.d/*.shAll usersDuring 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 ~/.bashrc for personal and safe variable definitions.
  • Use /etc/environment or /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​

VariablePurposeExample Value
$USERCurrent logged-in usernameroot
$HOMEHome directory/root
$SHELLDefault shell path/bin/bash
$PATHSearch directories for executables/usr/local/bin:/usr/bin:/bin
$PWDCurrent directory/root
$LANGLanguage/locale settingen_US.UTF-8
$EDITORDefault text editornano
$HISTSIZECommand history size10000

8. WordPress VPS Use Cases​

VariableExamplePurpose
WP_ENVexport WP_ENV=productionDistinguishes between staging/production environments.
WP_CLI_CACHE_DIR/root/.wp-cli/cacheSets WP-CLI’s global cache location.
DB_BACKUP_DIR/root/wpbackupCentralizes backup path for automation scripts.
EDITORnanoDefines default CLI editor for WP or config editing.
PATHPATH=$PATH:/root/wp-toolsAdds 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​

  1. Never store passwords or sensitive tokens in plain-text files like .bashrc.
  2. Use /etc/environment only for simple key=value pairs (no commands).
  3. Group related variables together logically.
  4. Always export variables that scripts or child processes will need.
  5. For WP automation, prefer descriptive naming (WP_ENV, WP_BACKUP_PATH).
  6. Test persistence after reboot or login/logout.
  7. Document variable purpose in comments.

11. Troubleshooting​

ProblemCauseSolution
Variable not available in new sessionFile not sourcedRun source ~/.bashrc
PATH not applied system-wideForgot to export or rebootAdd to /etc/profile.d/ and reload
Cron job not reading variableCron runs with limited environmentDefine variables directly inside cron or script
Variables overwrittenConflicts between filesCheck precedence order (system files load first)
Export errorIncorrect syntaxUse export VAR=value (no spaces around =)

12. Quick Lab​

Objective: Define and verify environment variables for WordPress automation.

  1. Edit .bashrc:
nano ~/.bashrc

  1. Add:
export DB_BACKUP_DIR="/root/wpbackup"
export WP_ENV="production"
export PATH=$PATH:/root/wp-tools

  1. Apply changes:
source ~/.bashrc

  1. Verify:
echo $WP_ENV
echo $DB_BACKUP_DIR
echo $PATH

  1. 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​

TermDefinition
Environment VariableA dynamic value that configures system or program behavior.
Shell VariableA variable local to the current shell (not exported).
ExportCommand to make a variable visible to child processes.
Persistent VariableA variable saved to configuration files that reload automatically.
SubshellA new shell instance that inherits exported variables.

14. Mini-Quiz​

  1. What’s the difference between set, env, and printenv?
  2. How do you make a variable available system-wide?
  3. What file is recommended for defining per-user environment variables?
  4. How do you make a variable available to child processes?
  5. 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/environment or /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)?