iCapSolutions

Cloud Computing – DevSecOps – IT Services

Bashrc Cheat Sheet

This bashrc cheat sheet collects the shell settings, aliases and functions we use on Linux servers and workstations, grouped by purpose so you can copy just the parts you want. Most of it works unchanged in zsh as well.

What Is .bashrc?

~/.bashrc is a script Bash runs every time you open an interactive, non-login shell, such as a new terminal window or tab. It’s where you put aliases, functions, prompt settings and history options. Its sibling ~/.bash_profile (or ~/.profile) runs for login shells, such as an SSH session; most setups have .bash_profile load .bashrc so the same settings apply everywhere:

# in ~/.bash_profile
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

After editing .bashrc, apply the changes to your current shell without opening a new one:

source ~/.bashrc

On macOS, the default shell is zsh, so the equivalent file is ~/.zshrc.

Bashrc Cheat Sheet: Quick Reference

SectionWhat it does
History and shell behaviorTimestamps history, skips duplicates, keeps 2,000 entries, appends instead of overwriting
Prompt colorsDefines color variables for a readable prompt
AliasesShort git commands and quick directory navigation
FunctionsHistory search, largest-file finder, repo bootstrap
Git promptShows the current branch in the prompt
Startup bannerPrints host and system details when a shell opens
CompletionCase-insensitive tab completion for bash and zsh

History and Shell Behavior

Core history retention and append behavior.

HISTTIMEFORMAT="%F %T "
HISTCONTROL=ignoredups
HISTSIZE=2000
HISTFILESIZE=2000

shopt -s histappend

Prompt Color Variables

Reusable terminal color variables used by the prompt.

blk='\[\033[01;30m\]'   # Black
red='\[\033[01;31m\]'   # Red
grn='\[\033[01;32m\]'   # Green
ylw='\[\033[01;33m\]'   # Yellow
blu='\[\033[01;34m\]'   # Blue
pur='\[\033[01;35m\]'   # Purple
cyn='\[\033[01;36m\]'   # Cyan
wht='\[\033[01;37m\]'   # White
clr='\[\033[00m\]'      # Reset

Git and Navigation Aliases

Daily aliases for git and directory navigation.

alias gs='git status'
alias ga='git add'
alias gaa='git add --all'
alias gc='git commit'
alias gl='git log --oneline'
alias gb='git checkout -b'
alias gd='git diff'
alias ..='cd ..;pwd'
alias ...='cd ../..;pwd'
alias ....='cd ../../..;pwd'
alias c='clear'
alias h='history'
alias tree='tree --dirsfirst -F'
alias mkdir='mkdir -p -v'

Calendar Aliases

Month shortcut aliases for fast calendar lookups.

alias jan='cal -m 01'
alias feb='cal -m 02'
alias mar='cal -m 03'
alias apr='cal -m 04'
alias may='cal -m 05'
alias jun='cal -m 06'
alias jul='cal -m 07'
alias aug='cal -m 08'
alias sep='cal -m 09'
alias oct='cal -m 10'
alias nov='cal -m 11'
alias dec='cal -m 12'

Search and File Utility Functions

Helpers for history search and largest-file inspection.

function hg() {
    history | grep "$1";
}

function find_largest_files() {
    du -h -x -s -- * | sort -r -h | head -20;
}

Git Branch and Prompt Setup

Prompt helpers that show git branch context in the shell prompt.

function git_branch() {
    if [ -d .git ] ; then
        printf "%s" "($(git branch 2> /dev/null | awk '/\*/{print $2}'))";
    fi
}

# Set the prompt.
function bash_prompt(){
    PS1='${debian_chroot:+($debian_chroot)}'${blu}'$(git_branch)'${pur}' \W'${grn}' \$ '${clr}
}

bash_prompt

Repository Bootstrap Function

Function to initialize a new repo scaffold quickly.

function git_init() {
    if [ -z "$1" ]; then
        printf "%s\n" "Please provide a directory name.";
    else
        mkdir "$1";
        builtin cd "$1";
        pwd;
        git init;
        touch readme.md .gitignore LICENSE;
        echo "# $(basename $PWD)" >> readme.md
    fi
}

Startup Terminal Summary Banner

Optional startup banner commands that print host/system details.

clear

printf "\n"
printf "   %s\n" "IP ADDR: $(curl ifconfig.me)"
printf "   %s\n" "USER: $(echo $USER)"
printf "   %s\n" "DATE: $(date)"
printf "   %s\n" "UPTIME: $(uptime -p)"
printf "   %s\n" "HOSTNAME: $(hostname -f)"
printf "   %s\n" "CPU: $(awk -F: '/model name/{print $2}' | head -1)"
printf "   %s\n" "KERNEL: $(uname -rms)"
printf "   %s\n" "PACKAGES: $(dpkg --get-selections | wc -l)"
printf "   %s\n" "RESOLUTION: $(xrandr | awk '/\*/{printf $1" "}')"
printf "   %s\n" "MEMORY: $(free -m -h | awk '/Mem/{print $3"/"$2}')"
printf "\n"

Note on the banner: curl ifconfig.me makes a network call every time a shell opens, which slows startup and fails offline. On servers you SSH into often, drop that line or add a short timeout such as curl -s --max-time 2 ifconfig.me.

Completion Settings

Bash and zsh completion configuration lines.

# for .bashrc
bind -s 'set completion-ignore-case on'

# for .zshrc
autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'

Tips for Keeping .bashrc Manageable

  • Test changes in a new terminal before closing your current one, so a syntax error doesn’t lock you out of a working shell.
  • Check syntax first with bash -n ~/.bashrc.
  • Split large configs into files such as ~/.bash_aliases and load them from .bashrc. Many Linux distributions already do this for aliases.
  • Keep secrets out of it. Tokens and passwords in .bashrc end up in backups and dotfile repositories; load them from a separate, permission-restricted file instead.
  • Version it. Keeping your dotfiles in a private Git repository makes setting up a new machine a two-minute job.

The GNU Bash manual’s section on startup files explains exactly which files run in which situations.

For broader Linux/DevOps reference beyond shell config, see our DevOps Cheat Sheet, and pair your shell setup with our Git Cheat Sheet.