Skip to main content

Per-Directory Management — Advanced Bash Customization


Learning Focus
  1. Understand the concept and purpose of per-directory configuration in Bash.
  2. Learn how to apply different behaviors or environments automatically when entering specific folders.
  3. Discover the main approaches — manual Bash logic, modular .bashrc.d/, and the direnv tool.
  4. View the directory structure (tree view) of user-level and WordPress-site-level setups.
  5. Apply per-directory configurations for WordPress VPS management, such as environment tags, PHP version checks, and automatic WP-CLI context.
  6. Follow best practices for performance, safety, and security when using contextual Bash setups.

1. What Is Per-Directory Management

Per-directory management means that Bash changes its configuration or behavior depending on the current working directory ($PWD). When you navigate (cd) into a directory, specific environment variables, prompts, or aliases can activate — and they can reset automatically when you leave that directory. Example goal:

  • Enter /home/dev-wpstrategist/public_html → prompt turns green and $WP_ENV becomes development.
  • Enter /home/prod-wpstrategist/public_html → prompt turns red and $WP_ENV becomes production.

2. Why Per-Directory Setup Matters

ReasonDescription
Context AwarenessAutomatically adapts configuration for each WordPress site.
SafetyHelps prevent running dangerous commands on production sites.
ConvenienceLoads custom aliases, variables, or functions for each directory.
AutomationReduces manual setup when managing multiple VPS WordPress installations.
IsolationKeeps each site’s environment independent and reproducible.

3. Where Configuration Lives

ScopeFileDescription
User-Level~/.bashrc.d/Modular Bash scripts loaded for each user.
Directory-Specific.envrc (via direnv)Automatically loads per-directory variables.
System-Level/etc/profile.d/Shared across all users and environments.

4. File Locations (Tree View)

User Configuration

/home/
└── donnyariw/
├── .bashrc
├── .bashrc.d/
│ ├── dev.sh
│ ├── stage.sh
│ └── prod.sh
├── .bash_profile
├── .bash_prompt
├── .bash_aliases
└── .bash_functions

WordPress VPS Example

/home/
├── dev-wpstrategist/
│ └── public_html/
│ └── .envrc ← dev-only configuration
├── stage-wpstrategist/
│ └── public_html/
│ └── .envrc
└── prod-wpstrategist/
└── public_html/
└── .envrc

5. Approach 1: Modular .bashrc.d/ Directory

a. Setup Modular Structure

Create directory:

mkdir ~/.bashrc.d

Inside ~/.bashrc, add loader logic:

# Load all modular Bash configurations
for file in ~/.bashrc.d/*.sh; do
[ -r "$file" ] && . "$file"
done

Now each .sh file under .bashrc.d/ acts as a sub-module.

b. Example: dev.sh

# ~/.bashrc.d/dev.sh
if [[ $PWD == *"dev-wpstrategist"* ]]; then
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
fi

c. Example: prod.sh

# ~/.bashrc.d/prod.sh
if [[ $PWD == *"prod-wpstrategist"* ]]; then
export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
fi

When you cd into /home/prod-wpstrategist/public_html, the prompt automatically changes to red and $WP_ENV updates.

6. Approach 2: Using direnv Tool (Automatic Loading)

direnv is a CLI utility that automatically loads environment variables when entering a directory and unloads them when you leave.

a. Installation

sudo apt install direnv -y

b. Enable for Bash

echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
source ~/.bashrc

c. Setup for WordPress Site

Example directory:

/home/dev-wpstrategist/public_html/.envrc

Content:

export WP_ENV=development
export PATH=$PATH:/home/dev-wpstrategist/tools
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '

Authorize:

direnv allow

Output:

direnv: loading ~/dev-wpstrategist/public_html/.envrc
direnv: export +WP_ENV +PATH +PS1

When you leave the directory:

direnv: unloading

✅ Variables revert automatically.

7. Approach 3: Scripted Detection Logic

If you prefer not to install additional tools, Bash itself can detect your current path and adjust behavior. Add this to .bashrc:

case "$PWD" in
*dev-wpstrategist*)
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
;;
*stage-wpstrategist*)
export WP_ENV=staging
export PS1='[\e[33m][STAGE: \u@\h \W]\$[\e[0m] '
;;
*prod-wpstrategist*)
export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
;;
esac

Reload:

source ~/.bashrc

Now your prompt automatically reflects the environment whenever you cd into any project directory.

8. WordPress VPS Use Cases

Use CaseExampleDescription
Environment IdentificationPrompt shows [DEV], [STAGE], [PROD]Prevents running production commands by mistake.
Custom PathsPATH=$PATH:/home/$USER/toolsLoads per-site tools or scripts.
WP-CLI Contextalias wp='wp --path=/home/dev-wpstrategist/public_html'Automatically points WP-CLI to correct path.
Backup Directory Mappingexport WP_BACKUP_DIR="/root/wpbackup/$WP_ENV"Keeps backups organized by environment.
Cache Behaviorexport LS_CACHE=1 or 0Enables or disables LSCache per environment.

9. Security and Performance Considerations

  1. Do not load sensitive credentials (like API keys) in .envrc unless permissions are restricted.
chmod 600 .envrc
  1. Avoid heavy logic inside .bashrc — Bash runs it at every session start.
  2. Disable per-directory overrides on shared servers unless isolation is guaranteed.
  3. Document each directory’s purpose — helps collaborators identify environments safely.
  4. **Back up .bashrc.d/ and **.envrc files before major updates.

10. Troubleshooting

ProblemCauseSolution
.envrc not loadingdirenv not authorizedRun direnv allow inside the directory.
Variables persist after leavingdirenv misconfiguredRe-load eval "$(direnv hook bash)".
Slow prompt renderingToo many conditions in .bashrcMove logic into .bashrc.d/ or .envrc.
PS1 not changing$PWD not matched properlyCheck case patterns or path expressions.

11. Quick Lab

Objective: Set up automatic prompt and variable change per environment directory.

  1. Install direnv:
sudo apt install direnv -y
  1. Add to .bashrc:
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
source ~/.bashrc
  1. Create project directories:
mkdir -p /home/{dev,stage,prod}-wpstrategist/public_html
  1. Add .envrc files: Development
export WP_ENV=development
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '

Production

export WP_ENV=production
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
  1. Authorize each:
cd /home/dev-wpstrategist/public_html && direnv allow
cd /home/prod-wpstrategist/public_html && direnv allow
  1. Verify:
echo $WP_ENV
echo $PS1

✅ Prompt color and environment variable change automatically per directory.

12. Glossary

TermDefinition
Per-Directory ConfigurationApplying different settings depending on the current directory.
direnvA tool that loads/unloads variables when entering/leaving directories.
.bashrc.dA folder-based modular approach for user-specific shell configurations.
WP_ENVA custom variable used to identify WordPress environments.
PS1The primary Bash prompt variable.

13. Mini-Quiz

  1. What command authorizes an .envrc file to load automatically?
  2. Where should .bashrc.d/ files be located?
  3. What’s the advantage of per-directory configuration for multi-site WordPress VPS setups?
  4. How does Bash detect which environment you’re in?
  5. Why should .envrc files have restricted permissions?

14. Summary

  • Per-directory management allows Bash to adapt automatically when entering specific directories.
  • Methods include modular .bashrc.d/ scripts, path detection in ****.bashrc, or direnv** automation**.
  • Ideal for WordPress VPS setups managing multiple environments (DEV, STAGE, PROD).
  • Enables safe, contextual automation with unique prompts and environment variables.
  • Maintain strict file permissions, avoid heavy logic in .bashrc, and document each environment for clarity and safety.