/ Show and Tell

My Bashrc

UPDATE: This is a bit dated. For one, I use ZSH with OH-MY-ZSH now. Only keeping this post alive for sentimental reasons.


It was only a matter of time before I posted something like this. Seems like every other day I see a similar post on /r/linux.

So, here is the contents of my ~/.bashrc. (Which by the way, all aspects are stored in a Git repository and written in such a way to have a single bashrc for multiple boxes and environments)

# .bashrc

if [ -n "$DISPLAY" -a "$TERM" == "xterm" ]; then
    export TERM=xterm-256color
fi

# Alias Directory
alias_dir=/home/quinn/Git/personal/configuration/mybashrc/.aliases

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

if [ -f /etc/skel/.bashrc ]; then
        . /etc/skel/.bashrc
fi

#-------------------------------------------------------------------------------
#  Case statement to set aliases and ps1 based on hostname
#-------------------------------------------------------------------------------
    case $(hostname) in
        "foo.work.bar"          ) source $alias_dir/work.alias;
                                  source $alias_dir/work.ps1;
                                  source $alias_dir/base.alias;;

        "foo.home.bar"          ) source $alias_dir/home.alias;
                                  source $alias_dir/home.ps1;
                                  source $alias_dir/base.alias;;

      "foo.home-ubuntu.bar"     ) source $alias_dir/home.alias;
                                  source $alias_dir/home.ps1;
                                  source $alias_dir/base.alias;;

        *                       ) source $alias_dir/base.alias;;
    esac

Pretty boring looking, but there is a bit more going on.

Firstly, the three if statements at the top are 'stock' and don't do a whole lot. I won't go into them unless someone wants me to.

Secondly, we have this variable: alias_dir=/home/quinn/Git/personal/configuration/mybashrc/.aliases. This is so I can set this variable and not have to change x amount of lines further down the file.

Then the case statement. As the comment block says, it sets my aliases and ps1 based on the output of the hostname command.

Lastly, the alias and ps1 files. I'll just cover the home.* versions of my configs. These can be named anything and have any valid .bashrc item inside.

# home.alias
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'

# home.ps1
RS="\[\033[0m\]"    # reset
HC="\[\033[1m\]"    # hicolor
UL="\[\033[4m\]"    # underline
INV="\[\033[7m\]"   # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white

whoisme=$(whoami)

if [[ "$whoisme"  == "root" ]]; then
     SMILEY="${FRED}ಠ_ಠ";
     MARK="$"
   else
     SMILEY="${FBLE}^◡^";
     MARK="#"
fi

#-------------------------------------------------------------------------------
#  PS1 and Git status
#-------------------------------------------------------------------------------
PS1="\n${SMILEY}${NONE}\[\e[32;1m\](\[\e[37;1m\]\u\[\e[32;1m\])-(\H)-(\[\e[37;1m\]\w\[\e[32;1m\])\$(parse_git_branch)\$(parse_git_dirty)\n--${MARK} \[\e[0m\]"

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

parse_git_dirty() {
  if [[ -d .git ]]; then
    [[ $(git status | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*"
    /bin/bash /home/quinn/.gitChecker.sh &
  fi
}

The home.alias is pretty lame. Just some aliases for chmod. The home.ps1 is a bit more interesting.

The beginning of the file sets color variables for my bash prompt. ALOT of them are not used, but I keep them set for referencing. Then, the whoisme check. Just a simple check for if the root user is at the prompt and sets variables accordingly. (My root user has the same bashrc as my mortal user)

The last section, PS1 and Git status, Sets the PS1 variable which uses the two functions at the bottom. Parse_git_branch gets the current branch and prints it in the PS1. Parse_git_dirty prints a * if the current git branch is dirty. The call to .gitChecker.sh just gets the last commit and prints info on it.

Put it all together and the output looks something like this!

screenshot1

Hopefully, this can help you make a .bashrc that you can be proud of!