Skip to main content

Advanced Setup — Bash Customization for Power Users


Learning Focus
  1. Understand how advanced setup features enhance Bash usability and automation.
  2. Master tab completion, per-directory configuration, system integration, and debugging.
  3. Learn where advanced configuration files live and how Bash loads them — shown in tree view.
  4. Customize Bash for WordPress VPS operations, including WP-CLI auto-completion and command safety.
  5. 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:

FeatureDescription
Tab CompletionAutomatically completes commands, files, or WP-CLI subcommands.
Per-Directory ManagementApplies custom settings per project or site directory.
System and Tools IntegrationSyncs with utilities like Git, WP-CLI, Redis, and Docker.
Debugging & TroubleshootingProvides visibility into command execution and errors.

2. Why Advanced Setup Matters

BenefitDescription
EfficiencyFaster navigation, fewer typos, context-aware suggestions.
ConsistencyUnified shell behavior across all WordPress sites.
AutomationSimplifies repetitive tasks using autoloading and completions.
SafetyReduces accidental destructive actions on production servers.
ProfessionalismCreates 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.

  1. Download it:
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -O /etc/bash_completion.d/wp
  1. Reload your shell:
source /etc/bash_completion.d/wp
  1. Test:
wp [TAB]

Output auto-completes available subcommands:

plugin theme user option db core cache

d. Other Common Completion Sources

ToolCompletion 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

  1. Keep completion scripts modular — separate files under /etc/bash_completion.d/.
  2. Don’t over-customize production servers — keep safety over visual design.
  3. Always test .bashrc edits with source ~/.bashrc before logout.
  4. Back up system configs:
sudo cp /etc/bash.bashrc /etc/bash.bashrc.bak
  1. Use clear directory-based indicators (e.g., [STAGE], [PROD]) in your PS1 prompt.
  2. 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.

  1. 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
  1. Create per-directory settings:
mkdir ~/.bashrc.d
nano ~/.bashrc.d/wpdev.sh

Add:

export PS1='[\e[32m][WP-DEV: \u@\h \W]\$[\e[0m] '
  1. Modify .bashrc:
for f in ~/.bashrc.d/*.sh; do
[ -r "$f" ] && . "$f"
done
  1. Apply:
source ~/.bashrc
  1. Test WP completion:
wp [TAB]

✅ Your prompt now adapts dynamically, and WP-CLI completions work instantly.

10. Glossary

TermDefinition
Tab CompletionAutomatically completes commands or file paths.
direnvTool that loads environment variables per directory.
Bash CompletionShell enhancement that adds context-aware autocompletion.
Debug ModeDisplays each command executed by Bash for troubleshooting.
PS4Debugging prompt variable used in Bash tracing (set -x).

11. Mini-Quiz

  1. Which command installs Bash completion on Ubuntu?
  2. Where do WP-CLI completion scripts reside by default?
  3. How do you enable directory-specific Bash configurations?
  4. What does bash -x do?
  5. 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/ or direnv to 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.