Advanced Setup — Bash Customization for Power Users
- Understand how advanced setup features enhance Bash usability and automation.
- Master tab completion, per-directory configuration, system integration, and debugging.
- Learn where advanced configuration files live and how Bash loads them — shown in tree view.
- Customize Bash for WordPress VPS operations, including WP-CLI auto-completion and command safety.
- Apply best practices for reliability, maintainability, and performance.
1. What Is “Advanced Setup” in Bash
Beyond .bashrc and aliases, advanced setup focuses on intelligent behavior and context awareness.
It allows Bash to dynamically adapt how commands behave depending on your current directory, active environment, or shell state.
Key capabilities include:
| Feature | Description |
|---|---|
| Tab Completion | Automatically completes commands, files, or WP-CLI subcommands. |
| Per-Directory Management | Applies custom settings per project or site directory. |
| System and Tools Integration | Syncs with utilities like Git, WP-CLI, Redis, and Docker. |
| Debugging & Troubleshooting | Provides visibility into command execution and errors. |
2. Why Advanced Setup Matters
| Benefit | Description |
|---|---|
| Efficiency | Faster navigation, fewer typos, context-aware suggestions. |
| Consistency | Unified shell behavior across all WordPress sites. |
| Automation | Simplifies repetitive tasks using autoloading and completions. |
| Safety | Reduces accidental destructive actions on production servers. |
| Professionalism | Creates a standardized, developer-grade Bash environment. |
3. File Locations (Tree View)
User-Level Configuration
/home/
└── donnyariw/
├── .bashrc
├── .bash_profile
├── .bash_aliases
├── .bash_functions
├── .bash_prompt
└── .inputrc ← tab-completion and keyboard behavior
System-Wide Configuration
/etc/
├── bash.bashrc
├── profile
├── bash_completion.d/ ← command-specific completions
└── profile.d/
├── lang.sh
├── wp-completion.sh
├── docker-completion.sh
└── custom-debug.sh
4. Tab Completion
a. What It Does
Tab completion lets you press Tab to auto-complete commands, paths, or options. Example:
cd /var/ww[TAB]
becomes:
cd /var/www/
b. Enable Bash Completion (System-Wide)
Install the package:
sudo apt install bash-completion -y
Check that it’s loaded:
echo $BASH_COMPLETION
If not, ensure these lines exist in /etc/bash.bashrc or ~/.bashrc:
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
Reload:
source ~/.bashrc
c. WP-CLI Tab Completion
WP-CLI provides an official completion script for WordPress command automation.
- Download it:
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -O /etc/bash_completion.d/wp
- Reload your shell:
source /etc/bash_completion.d/wp
- Test:
wp [TAB]
Output auto-completes available subcommands:
plugin theme user option db core cache
d. Other Common Completion Sources
| Tool | Completion Script Location |
|---|---|
| Git | /usr/share/bash-completion/completions/git |
| Docker | /etc/bash_completion.d/docker |
| Kubernetes (kubectl) | /etc/bash_completion.d/kubectl |
| WP-CLI | /etc/bash_completion.d/wp |
You can enable or disable each by commenting its line in /etc/bash_completion. |
5. Per-Directory Bash Configuration
Per-directory settings let Bash behave differently depending on your working directory — ideal for multi-site WordPress servers.
a. Using .bashrc.d Structure
You can modularize your .bashrc by adding per-directory configuration files.
Example:
/home/donnyariw/.bashrc.d/
├── dev.sh
├── stage.sh
└── prod.sh
Inside .bashrc:
for f in ~/.bashrc.d/*.sh; do
[ -r "$f" ] && . "$f"
done
Now, each site or project can have unique behaviors, such as colorized PS1 or environment tags.
b. Using direnv (optional tool)
For complex setups, direnv loads environment variables automatically when you enter a directory.
Install:
sudo apt install direnv -y
Hook it into Bash:
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
Example .envrc file in a WordPress directory:
export WP_ENV=staging
export PATH=$PATH:/home/stage/tools
When you cd into that directory, those variables activate automatically.
6. System and Tools Integration
a. Integration with Git
Add to .bashrc:
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\u@\h \W$(__git_ps1 " (%s)")\$ '
This shows the active Git branch in your prompt:
root@wpstrategist html (main)#
b. Integration with Redis and WP-CLI
Define helper functions:
redisinfo() { redis-cli info | grep used_memory_human; }
wpstatus() { wp plugin list --allow-root; }
Now these are callable instantly from any directory.
c. Integration with Docker
If you manage containers:
alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
7. Debugging and Troubleshooting Bash
a. Enable Debug Mode
Run a script with trace output:
bash -x script.sh
Each executed command prints with a + prefix.
b. Enable Verbose Execution
bash -v script.sh
Displays commands as they are read, before execution.
c. Inline Debug Mode
To trace specific commands:
set -x # enable
# commands to debug
set +x # disable
Example (WordPress backup trace):
set -x
wp db export /root/wpbackup/db.sql --allow-root
set +x
8. Best Practices
- Keep completion scripts modular — separate files under
/etc/bash_completion.d/. - Don’t over-customize production servers — keep safety over visual design.
- Always test
.bashrcedits withsource ~/.bashrcbefore logout. - Back up system configs:
sudo cp /etc/bash.bashrc /etc/bash.bashrc.bak
- Use clear directory-based indicators (e.g.,
[STAGE],[PROD]) in your PS1 prompt. - Limit heavy logic in PS1 — large commands can slow prompt rendering.
9. Quick Lab
Objective: Enable WP-CLI tab completion and modular .bashrc.d setup.
- Install completion:
sudo apt install bash-completion -y
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -O /etc/bash_completion.d/wp
- Create per-directory settings:
mkdir ~/.bashrc.d
nano ~/.bashrc.d/wpdev.sh
Add:
export PS1='[\e[32m][WP-DEV: \u@\h \W]\$[\e[0m] '
- Modify
.bashrc:
for f in ~/.bashrc.d/*.sh; do
[ -r "$f" ] && . "$f"
done
- Apply:
source ~/.bashrc
- Test WP completion:
wp [TAB]
✅ Your prompt now adapts dynamically, and WP-CLI completions work instantly.
10. Glossary
| Term | Definition |
|---|---|
| Tab Completion | Automatically completes commands or file paths. |
| direnv | Tool that loads environment variables per directory. |
| Bash Completion | Shell enhancement that adds context-aware autocompletion. |
| Debug Mode | Displays each command executed by Bash for troubleshooting. |
| PS4 | Debugging prompt variable used in Bash tracing (set -x). |
11. Mini-Quiz
- Which command installs Bash completion on Ubuntu?
- Where do WP-CLI completion scripts reside by default?
- How do you enable directory-specific Bash configurations?
- What does
bash -xdo? - Why should you avoid putting heavy logic inside PS1?
12. Summary
- Advanced Bash setup extends your environment with dynamic completion, per-directory control, and better debugging.
- Store completion scripts in
/etc/bash_completion.d/and load them via/etc/bash.bashrc. - Use
.bashrc.d/ordirenvto apply per-site or per-project behavior on a WordPress VPS. - Integrate tools like Git, Docker, and WP-CLI for intelligent automation.
- Keep configurations modular, test them with
source, and maintain backups for reliability.