Exploring My .bashrc In 2021

Exploring My .bashrc In 2021

I read a great post on HN written by Lei Mao talking about the differences between the various "profile" scripts - .bashrc, .profile, .bash_profile - and it made me realize just how long it's been since I looked at my personal Bash profile. I figure the beginning of the year is as good a time as any to take a walk through my personal .bashrc file and see if I can even remember what everything does ;) If you'd like a copy to follow along or for your own personal use, here you go - my bashrc.

I want to start with a couple of caveats - first, I'm sure my profile isn't nearly as interesting or cutting-edge as some others. Yes, it's kind of simple, but I've been using it for almost 7 years now and it's performed solidly. Second, in order to use it on your box you will need to install a couple of applications (we'll go over those below). Finally, I'm a Debian guy and so is my profile; it should work on the most recent Debian-based distros including Ubuntu, but YMMV (if I can find my old Gentoo .bashrc I'll write it up in a separate article).

Ok, so let's jump right in!

First off, a couple of comments and then we set the prompts - green and blue and "$" for regular users, red and blue and "#" for root. As you can see from the debian_chroot reference, this section is a leftover from the original Debian file:

# Matt's .bashrc
# System Requirements: color-capable terminal

if [ $EUID -eq 0 ]; then
	PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]\[\033[01;34m\] [\w]\[\033[00m\]\$ '
else	
	PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]\[\033[01;34m\] [\w]\[\033[00m\]\$ '
fi

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
Configure the Prompt

Next, I like my terminals to have a lot of color. By aliasing some commands and using dircolors and the application grc I can add color to some of the commands I use most frequently:

# Alias grep to egrep and configure color output and line numbers
alias grep="egrep --color=always -n"

# Colorize output from ls
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'

#Configure grc to provide colorized output for system commands
GRC=`which grc`
alias colourify="$GRC -es --colour=auto"
alias netstat='colourify netstat'
alias ping='colourify ping'
alias traceroute='colourify traceroute'
alias dig='colourify dig'
alias df='colourify df'
alias mount='colourify mount'
alias ps='colourify ps'
Colorful Output

Next up, due to my tendency to work long hours and general brain fuzziness, I set up some bumper rails for myself:

# Alias commands for safety
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
Safety Bumpers

Finally, I added two other function aliases that I found myself using frequently - rsync-over-SSH and using the pandoc command to read Markdown files in the terminal:

# Create rsync-over-SSH function command, using SSH port tcp/22222.
function rsynssh() {
	rsync -r -a -v -e "ssh -p 22222" --progress $1 $2
}

# Create a function to read Markdown files in the terminal 
function rmd() {
	pandoc $1 | lynx -stdin
}
Function aliases

If you're wanting to recreate my .bashrc on your system, you will need to install the applications grc, rsync, and pandoc (I'm assuming SSH is already installed):

apt-get install grc rsync pandoc

It's not flashy and it may not win any awards, but it's one of the fundamental tools in my toolkit. Anytime I'm building a new server, after setting up my .ssh directory and authorized_keys file, I install the helper apps and copy over my .bashrc. If nothing else, feel free to use my example to build a bigger and better profile to suit your daily admin needs.