Making .bashrc modular (and awesome)
.bashrc files quickly get complex.
Putting them into revision control is handy for persistance, but not always the best for sharing when not everyone has the same environment.
So lets fix that!
Base Script
Replace the .bashrc file with a simple script that will run other scripts.
~/.bashrc
#!/bin/bash
#-------------------------------------------------------------
# Modular bash scripts
#-------------------------------------------------------------
# iterate over our bashrc script files
for script in bashrc_scripts/*.sh
do
# check if the script is executable
if [ -x "${script}" ]; then
# run the script
source ${script}
fi
done
Now create the directory ~/bashrc_scripts and begin adding scripts in there.
Scripts must be executable to be included. So to enable a script simply make it executable:
chmod +x bashrc_scripts/[script name]
Example Scripts
The following are the scripts that I’m currently using.
bash.sh – Sets nice defaults for bash environment
#!/bin/bash
#-------------------------------------------------------------
# Source global definitions (if any)
#-------------------------------------------------------------
if [ -f /etc/bashrc ]; then
# Read /etc/bashrc if present.
. /etc/bashrc
fi
# don't put duplicate commands in bash history
export HISTCONTROL=erasedups
# expand the bash history to X commands
export HISTSIZE=5000
brew.sh – Script for adding Mac Brew completion to bash
#!/bin/bash
#-------------------------------------------------------------
# Brew for OS-X support
#-------------------------------------------------------------
# bash completion
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
python_brew.sh – Script for adding pythonbrew bash completion
#!/bin/bash
#-------------------------------------------------------------
# Python definitions
#-------------------------------------------------------------
# Pythonbrew
# add pythonbrew support
if [[ -s $HOME/.pythonbrew/etc/bashrc ]]; then
source $HOME/.pythonbrew/etc/bashrc
fi
python_virtualenv.sh – Script to add virtualenv / virtualenvwrapper support and bash completion
#!/bin/bash
#-------------------------------------------------------------
# Python definitions
#-------------------------------------------------------------
# Virtualenvwrapper
# virtualenv wrapper support
export WORKON_HOME=~/Workspace/VirtualEnvs
if [[ -s /usr/local/bin/virtualenvwrapper.sh ]]; then
source /usr/local/bin/virtualenvwrapper.sh
fi
# PIP
# tell pip to only install inside virtualenvs
export PIP_REQUIRE_VIRTUALENV=true
# make pip use the virtualenv dir
export PIP_VIRTUALENV_BASE=$WORKON_HOME
# add pip bash completion
# use eval to avoid the error "Could not find an activated virtualenv (required)."
eval `PIP_REQUIRE_VIRTUALENV= pip completion --bash`
2012/03/19 at 3:57 pm
[…] Follow this guide if you want to make your .bashrc modular. […]