.bashrc Configuration Cheat Sheet

Purpose

The .bashrc file is executed for interactive non-login shells. It is commonly used to set environment variables, aliases, shell options, and prompt customizations.

Disclosure: Initially Generated with ChatGPT, updated manually over time.


Table of Contents

  1. .bashrc Configuration Cheat Sheet
    1. Purpose
  2. Table of Contents
    1. Common Configurations
      1. 1. Aliases
      2. 2. Environment Variables
      3. 3. Shell Options
      4. 4. Prompt Customization
    2. History Settings
    3. Path Management
    4. Functions
    5. Load Custom Scripts
    6. Conditional Logic
    7. Best Practices
    8. Reloading .bashrc
  3. Useful snippets
    1. Remove Highlight from Directories in ls

Common Configurations

1. Aliases

alias ll='ls -lah'
alias gs='git status'
alias ..='cd ..'

2. Environment Variables

export EDITOR=nano
export PATH="$HOME/bin:$PATH"

3. Shell Options

shopt -s histappend    # Append to history instead of overwriting
shopt -s autocd        # Allow entering directories without `cd`

4. Prompt Customization

# Basic prompt
export PS1="\u@\h:\w\$ "

# Colored prompt
export PS1="\[\e[32m\]\u@\h\[\e[m\]:\[\e[34m\]\w\[\e[m\]\$ "

History Settings

HISTSIZE=1000
HISTFILESIZE=2000
HISTCONTROL=ignoredups:erasedups

Path Management

# Add custom directory to PATH
export PATH="$HOME/scripts:$PATH"

Functions

mkcd () {
  mkdir -p "$1"
  cd "$1"
}

Load Custom Scripts

# Source other config files
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
[ -f ~/.bash_exports ] && . ~/.bash_exports

Conditional Logic

if [[ $EUID -eq 0 ]]; then
  echo "You are root"
fi

Best Practices

  • Keep .bashrc modular by splitting into separate files like .bash_aliases, .bash_exports
  • Always backup before editing: cp ~/.bashrc ~/.bashrc.bak
  • Reload after editing: source ~/.bashrc

Reloading .bashrc

source ~/.bashrc

Useful snippets

Remove Highlight from Directories in ls

Add this line into your bashrc, it keeps the color of directories but removes the bright green highlight

export LS_COLORS="di=34:ln=36:so=32:pi=33:ex=31:bd=33;01:cd=33;01:su=37;41:sg=30;43:tw=30;42:ow=00" # Removes annoying highlighting from ls